How to convert a currency string to a double with

2019-01-01 03:33发布

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.

14条回答
笑指拈花
2楼-- · 2019-01-01 04:20

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.

查看更多
后来的你喜欢了谁
3楼-- · 2019-01-01 04:21
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;
}
查看更多
登录 后发表回答