How to convert a currency string to a double with jQuery or Javascript?

后端 未结 19 2466
天涯浪人
天涯浪人 2020-11-22 17:30

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,1

相关标签:
19条回答
  • 2020-11-22 18:00
    jQuery.preferCulture("en-IN");
    var price = jQuery.format(39.00, "c");
    

    output is: Rs. 39.00

    use jquery.glob.js,
        jQuery.glob.all.js
    
    0 讨论(0)
  • 2020-11-22 18:01

    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$")

    0 讨论(0)
  • 2020-11-22 18:04

    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'));
    console.log(number.toFixed(2));

    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.

    0 讨论(0)
  • 2020-11-22 18:07

    This function should work whichever the locale and currency settings :

    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('.');
    }
    

    This assumes you know the decimal point character (in my case the locale is set from PHP, so I get it with <?php echo cms_function_to_get_decimal_point(); ?>).

    0 讨论(0)
  • 2020-11-22 18:08
    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;
    }
    
    0 讨论(0)
  • 2020-11-22 18:09

    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;
    
    0 讨论(0)
提交回复
热议问题