可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have a text box that will have a currency string in it that I then need to convert that string to a double to perform some operations on it.
\"$1,100.00\" -> 1100.00
This needs to occur all client side. I have no choice but to leave the currency string as a currency string as input but need to cast/convert it to a double to allow some mathematical operations.
回答1:
Remove all non dot / digits:
var currency = \"-$4,400.50\";
var number = Number(currency.replace(/[^0-9.-]+/g,\"\"));
回答2:
accounting.js is the way to go. I used it at a project and had very good experience using it.
accounting.formatMoney(4999.99, \"€\", 2, \".\", \",\"); // €4.999,99
accounting.unformat(\"€ 1.000.000,00\", \",\"); // 1000000
You can find it at GitHub
回答3:
Use a regex to remove the formating (dollar and comma), and use parseFloat to convert the string to a floating point number.`
var currency = \"$1,100.00\";
currency.replace(/[$,]+/g,\"\");
var result = parseFloat(currency) + .05;
回答4:
I know this is an old question but wanted to give an additional option.
The jQuery Globalize gives the ability to parse a culture specific format to a float.
https://github.com/jquery/globalize
Given a string \"$13,042.00\", and Globalize set to en-US:
Globalize.culture(\"en-US\");
You can parse the float value out like so:
var result = Globalize.parseFloat(Globalize.format(\"$13,042.00\", \"c\"));
This will give you:
13042.00
And allows you to work with other cultures.
回答5:
This example run ok
var currency = \"$123,456.00\";
var number = Number(currency.replace(/[^0-9\\.]+/g,\"\"));
alert(number);
http://jsbin.com/ecAviVOV/2/edit
回答6:
I know this is an old question, but CMS\'s answer seems to have one tiny little flaw: it only works if currency format uses \".\" as decimal separator.
For example, if you need to work with russian rubles, the string will look like this:
\"1 000,00 rub.\"
My solution is far less elegant than CMS\'s, but it should do the trick.
var currency = \"1 000,00 rub.\"; //it works for US-style currency strings as well
var cur_re = /\\D*(\\d+|\\d.*?\\d)(?:\\D+(\\d{2}))?\\D*$/;
var parts = cur_re.exec(currency);
var number = parseFloat(parts[1].replace(/\\D/,\'\')+\'.\'+(parts[2]?parts[2]:\'00\'));
Assumptions:
- currency value uses decimal notation
- there are no digits in the string that are not a part of the currency value
- currency value contains either 0 or 2 digits in its fractional part *
The regexp can even handle something like \"1,999 dollars and 99 cents\", though it isn\'t an intended feature and it should not be relied upon.
Hope this will help someone.
回答7:
You can try this
<script type=\"text/javascript\">
var str=\"$1,112.12\";
str = str.replace(\",\",\"\");
str = str.replace(\"$\",\"\");
document.write(parseFloat(str));
</script>
回答8:
I know you\'ve found a solution to your question, I just wanted to recommend that maybe you look at the following more extensive jQuery plugin for International Number Formats:
International Number Formatter
回答9:
jQuery.preferCulture(\"en-IN\");
var price = jQuery.format(39.00, \"c\");
output is: Rs. 39.00
use jquery.glob.js,
jQuery.glob.all.js
回答10:
// \"10.000.500,61 TL\" price_to_number => 10000500.61
// \"10000500.62\" number_to_price => 10.000.500,62
JS FIDDLE: https://jsfiddle.net/Limitlessisa/oxhgd32c/
var price=\"10.000.500,61 TL\";
document.getElementById(\"demo1\").innerHTML = price_to_number(price);
var numberPrice=\"10000500.62\";
document.getElementById(\"demo2\").innerHTML = number_to_price(numberPrice);
function price_to_number(v){
if(!v){return 0;}
v=v.split(\'.\').join(\'\');
v=v.split(\',\').join(\'.\');
return Number(v.replace(/[^0-9.]/g, \"\"));
}
function number_to_price(v){
if(v==0){return \'0,00\';}
v=parseFloat(v);
v=v.toFixed(2).replace(/(\\d)(?=(\\d\\d\\d)+(?!\\d))/g, \"$1,\");
v=v.split(\'.\').join(\'*\').split(\',\').join(\'.\').split(\'*\').join(\',\');
return v;
}
回答11:
This is my function. Works with all currencies..
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]/, \"\")
);
}
Usage : toFloat(\"$1,100.00\")
or toFloat(\"1,100.00$\")
回答12:
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;
}
回答13:
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;
}
回答14:
$ 150.00
Fr. 150.00
€ 689.00
I have tested for above three currency symbols .You can do it for others also.
var price = Fr. 150.00;
var priceFloat = price.replace(/[^\\d\\.]/g, \'\');
Above regular expression will remove everything that is not a digit or a period.So You can get the string without currency symbol but in case of \" Fr. 150.00 \" if you console for output then you will get price as
console.log(\'priceFloat : \'+priceFloat);
output will be like priceFloat : .150.00
which is wrong so you check the index of \".\" then split that and get the proper result.
if (priceFloat.indexOf(\'.\') == 0) {
priceFloat = parseFloat(priceFloat.split(\'.\')[1]);
}else{
priceFloat = parseFloat(priceFloat);
}