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

后端 未结 19 2439
天涯浪人
天涯浪人 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 17:44

    You can try this

    var str = "$1,112.12";
    str = str.replace(",", "");
    str = str.replace("$", "");
    console.log(parseFloat(str));

    0 讨论(0)
  • 2020-11-22 17:44

    Such a headache and so less consideration to other cultures for nothing...

    here it is folks:

    let floatPrice = parseFloat(price.replace(/(,|\.)([0-9]{3})/g,'$2').replace(/(,|\.)/,'.'));
    

    as simple as that.

    0 讨论(0)
  • 2020-11-22 17:45

    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

    0 讨论(0)
  • 2020-11-22 17:45

    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

    0 讨论(0)
  • 2020-11-22 17:47

    This worked for me and covers most edge cases :)

    function toFloat(num) {
      const cleanStr = String(num).replace(/[^0-9.,]/g, '');
      let dotPos = cleanStr.indexOf('.');
      let commaPos = cleanStr.indexOf(',');
    
      if (dotPos < 0) dotPos = 0;
    
      if (commaPos < 0) commaPos = 0;
    
      const dotSplit = cleanStr.split('.');
      const commaSplit = cleanStr.split(',');
    
      const isDecimalDot = dotPos
        && (
          (commaPos && dotPos > commaPos)
          || (!commaPos && dotSplit[dotSplit.length - 1].length === 2)
        );
    
      const isDecimalComma = commaPos
        && (
          (dotPos && dotPos < commaPos)
          || (!dotPos && commaSplit[commaSplit.length - 1].length === 2)
        );
    
      let integerPart = cleanStr;
      let decimalPart = '0';
      if (isDecimalComma) {
        integerPart = commaSplit[0];
        decimalPart = commaSplit[1];
      }
      if (isDecimalDot) {
        integerPart = dotSplit[0];
        decimalPart = dotSplit[1];
      }
    
      return parseFloat(
        `${integerPart.replace(/[^0-9]/g, '')}.${decimalPart.replace(/[^0-9]/g, '')}`,
      );
    }
    
    
    toFloat('USD 1,500.00'); // 1500
    toFloat('USD 1,500'); // 1500
    toFloat('USD 500.00'); // 500
    toFloat('USD 500'); // 500
    
    toFloat('EUR 1.500,00'); // 1500
    toFloat('EUR 1.500'); // 1500
    toFloat('EUR 500,00'); // 500
    toFloat('EUR 500'); // 500
    
    0 讨论(0)
  • 2020-11-22 17:47
        $ 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);
        }
    
    0 讨论(0)
提交回复
热议问题