如何使用jQuery或Javascript将货币字符串转换为double?
我有一个文本框,其中包含一个货币字符串,然后我需要将该字符串转换为double来对其执行一些操作如何使用jQuery或Javascript将货币字符串转换为double?,javascript,jquery,Javascript,Jquery,我有一个文本框,其中包含一个货币字符串,然后我需要将该字符串转换为double来对其执行一些操作 “$1100.00”→ 1100.00 这需要在所有客户端进行。我别无选择,只能将货币字符串保留为货币字符串作为输入,但需要将其转换为双精度,以允许进行一些数学运算 我知道您已经找到了问题的解决方案,我只是想建议您看看以下针对国际数字格式的更广泛的jQuery插件: 你可以试试这个 var str=“$1112.12”; str=str.replace(“,”,”); str=str.replac
“$1100.00”
→ <代码>1100.00
这需要在所有客户端进行。我别无选择,只能将货币字符串保留为货币字符串作为输入,但需要将其转换为双精度,以允许进行一些数学运算 我知道您已经找到了问题的解决方案,我只是想建议您看看以下针对国际数字格式的更广泛的jQuery插件: 你可以试试这个
var str=“$1112.12”;
str=str.replace(“,”,”);
str=str.replace(“$”,”);
log(parseFloat(str))代码>使用正则表达式删除格式设置(美元和逗号),并使用parseFloat将字符串转换为浮点数`
var currency = "$1,100.00";
currency.replace(/[$,]+/g,"");
var result = parseFloat(currency) + .05;
删除所有非点/数字:
var currency = "-$4,400.50";
var number = Number(currency.replace(/[^0-9.-]+/g,""));
我知道这是一个老问题,但我想给你一个额外的选择
jqueryglobalize提供了将特定于区域性的格式解析为float的能力
给定字符串“$13042.00”,并将Globalize设置为en US:
Globalize.culture("en-US");
您可以这样解析浮点值:
var result = Globalize.parseFloat(Globalize.format("$13,042.00", "c"));
这将为您提供:
13042.00
并允许您与其他文化合作 我知道这是一个老问题,但CMS的答案似乎有一个小小的缺陷:只有当货币格式使用“.”作为十进制分隔符时,它才有效。
例如,如果需要使用俄罗斯卢布,字符串将如下所示:
“1000,00卢布。”
我的解决方案远不如CMS的优雅,但它应该可以做到这一点
var currency=“1000,00卢布”//它也适用于美式货币字符串
var cur_re=/\D*(\D+\D.*?\D)(?:\D+(\D{2}))?\D*$/;
var部件=当前执行(货币);
var number=parseFloat(部分[1]。替换(/\D/,'')+。+(部分[2]?部分[2]:'00');
控制台日志(编号:toFixed(2))代码>是一条路要走。我在一个项目中使用了它,并且有很好的使用经验
accounting.formatMoney(4999.99, "€", 2, ".", ","); // €4.999,99
accounting.unformat("€ 1.000.000,00", ","); // 1000000
您可以在中找到该示例运行ok
var currency=“$1123456.00”;
变量编号=编号(货币替换(/[^0-9\.]+/g,”);
控制台日志(编号)代码>
产出为:39.00卢比
use jquery.glob.js,
jQuery.glob.all.js
我已经测试了以上三种货币符号。你也可以为其他货币符号测试
var price = Fr. 150.00;
var priceFloat = price.replace(/[^\d\.]/g, '');
上面的正则表达式将删除所有不是数字或句点的内容。因此,您可以获得不带货币符号的字符串,但如果是“Fr.150.00”,则如果您使用控制台进行输出,那么您将获得价格作为
console.log('priceFloat : '+priceFloat);
output will be like priceFloat : .150.00
这是错误的,所以你要检查“.”的索引,然后将其拆分,得到正确的结果
if (priceFloat.indexOf('.') == 0) {
priceFloat = parseFloat(priceFloat.split('.')[1]);
}else{
priceFloat = parseFloat(priceFloat);
}
这是我的职责。适用于所有货币
function toFloat(num) {
dotPos = num.indexOf('.');
commaPos = num.indexOf(',');
if (dotPos < 0)
dotPos = 0;
if (commaPos < 0)
commaPos = 0;
if ((dotPos > commaPos) && dotPos)
sep = dotPos;
else {
if ((commaPos > dotPos) && commaPos)
sep = commaPos;
else
sep = false;
}
if (sep == false)
return parseFloat(num.replace(/[^\d]/g, ""));
return parseFloat(
num.substr(0, sep).replace(/[^\d]/g, "") + '.' +
num.substr(sep+1, num.length).replace(/[^0-9]/, "")
);
}
函数toFloat(num){
dotPos=num.indexOf('.');
逗号=num.indexOf(',');
if(dotPos<0)
dotPos=0;
如果(逗号<0)
逗号=0;
如果((dotPos>commaPos)和&dotPos)
sep=dotPos;
否则{
如果((commaPos>dotPos)和&commaPos)
sep=逗号;
其他的
sep=假;
}
如果(sep==false)
返回parseFloat(num.replace(/[^\d]/g,”);
返回浮点(
num.substr(0,sep).replace(/[^\d]/g,“”)+'.+
num.substr(sep+1,num.length)。替换(/[^0-9]/,“”)
);
}
用法:toFloat($1100.00)
或toFloat($1100.00)
/“10.000.500,61 TL”价格单位编号=>10000500.61
//“10000500.62”编号至价格=>10.000.500,62
JS小提琴:
无论区域设置和货币设置为:
function getNumPrice(price, decimalpoint) {
var p = price.split(decimalpoint);
for (var i=0;i<p.length;i++) p[i] = p[i].replace(/\D/g,'');
return p.join('.');
}
函数getNumPrice(价格,小数点){
var p=价格分割(分点);
对于(var i=0;i这对我有效,并且涵盖了大多数边缘情况:)
函数toFloat(num){
const cleanStr=String(num).replace(/[^0-9.,]/g',);
设dotPos=cleanStr.indexOf('.');
设逗号=cleanStr.indexOf(',');
如果(dotPos<0)dotPos=0;
如果(逗号<0)逗号=0;
const dotSplit=cleanStr.split('.');
常量commaSplit=cleanStr.split(',');
常数isDecimalDot=dotPos
&& (
(commaPos和&dotPos>commaPos)
||(!commaPos&&dotSplit[dotSplit.length-1].length==2)
);
常量IsDecimal逗号=逗号
&& (
(dotPos和&dotPos
toFloat('USD 1500.00');//1500
托夫洛特('USD 1500');//1500
托夫洛特('USD 500.00');//500
托夫洛特('USD 500');//500
托夫洛特('1500.00欧元);//1500
托福乐特('1500欧元);//1500
托福乐特('500,00欧元);//500
托福乐(500欧元);//500
这里有一个简单的函数-
函数getNumberFromCurrency(货币){
返回编号(货币替换(/[$,]/g,,))
}
console.log(getNumberFromCurrency(“$1000000.99”)//1000000.99
适用于在2021中寻找解决方案的任何人,您可以使用
经过大量研究,这是我发现的最可靠的生产方法,到目前为止我没有任何问题。此外,它在Github上非常活跃
currency(123); // 123.00
currency(1.23); // 1.23
currency("1.23") // 1.23
currency("$12.30") // 12.30
var value = currency("123.45");
currency(value); // 123.45
如此令人头痛,对其他文化的考虑如此之少
下面是各位:
let floatPrice=parseFloat(price.replace(/(,| \.)([0-9]{3})/g,“$2”).replace(/(,| \.)/,”);
就这么简单。值得注意的是,当你添加货币时,你不应该在任何非“玩具”应用程序中使用float。你最终会得到不准确的总数。当parseFloat(“151.20”*100)给你15119.99999999998,而parseFloat(“151.40”*100)给你15119.99999998时,你会感到惊讶和不高兴
let thousands_seps = '.';
let decimal_sep = ',';
let sanitizeValue = "R$ 2.530,55".replace(thousands_seps,'')
.replace(decimal_sep,'.')
.replace(/[^0-9.-]+/, '');
// Converting to float
// Result 2530.55
let stringToFloat = parseFloat(sanitizeValue);
// Formatting for currency: "R$ 2.530,55"
// BRL in this case
let floatTocurrency = Number(stringToFloat).toLocaleString('pt-BR', {style: 'currency', currency: 'BRL'});
// Output
console.log(stringToFloat, floatTocurrency);
function NumberConvertToDecimal (number) {
if (number == 0) {
return '0.00';
}
number = parseFloat(number);
number = number.toFixed(2).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1");
number = number.split('.').join('*').split('*').join('.');
return number;
}
var parseCurrency = function (e) {
if (typeof (e) === 'number') return e;
if (typeof (e) === 'string') {
var str = e.trim();
var value = Number(e.replace(/[^0-9.-]+/g, ""));
return str.startsWith('(') && str.endsWith(')') ? -value: value;
}
return e;
}
function getNumPrice(price, decimalpoint) {
var p = price.split(decimalpoint);
for (var i=0;i<p.length;i++) p[i] = p[i].replace(/\D/g,'');
return p.join('.');
}
currency(123); // 123.00
currency(1.23); // 1.23
currency("1.23") // 1.23
currency("$12.30") // 12.30
var value = currency("123.45");
currency(value); // 123.45
let thousands_seps = '.';
let decimal_sep = ',';
let sanitizeValue = "R$ 2.530,55".replace(thousands_seps,'')
.replace(decimal_sep,'.')
.replace(/[^0-9.-]+/, '');
// Converting to float
// Result 2530.55
let stringToFloat = parseFloat(sanitizeValue);
// Formatting for currency: "R$ 2.530,55"
// BRL in this case
let floatTocurrency = Number(stringToFloat).toLocaleString('pt-BR', {style: 'currency', currency: 'BRL'});
// Output
console.log(stringToFloat, floatTocurrency);