Convert String with Dot or Comma as decimal separator to number in JavaScript

后端 未结 11 727
时光取名叫无心
时光取名叫无心 2020-12-03 04:26

An input element contains numbers a where comma or dot is used as decimal separator and space may be used to group thousands like this:

\'1,2\'

相关标签:
11条回答
  • 2020-12-03 04:50

    All the other solutions require you to know the format in advance. I needed to detect(!) the format in every case and this is what I end up with.

    function detectFloat(source) {
        let float = accounting.unformat(source);
        let posComma = source.indexOf(',');
        if (posComma > -1) {
            let posDot = source.indexOf('.');
            if (posDot > -1 && posComma > posDot) {
                let germanFloat = accounting.unformat(source, ',');
                if (Math.abs(germanFloat) > Math.abs(float)) {
                    float = germanFloat;
                }
            } else {
                // source = source.replace(/,/g, '.');
                float = accounting.unformat(source, ',');
            }
        }
        return float;
    }
    

    This was tested with the following cases:

            const cases = {
                "0": 0,
                "10.12": 10.12,
                "222.20": 222.20,
                "-222.20": -222.20,
                "+222,20": 222.20,
                "-222,20": -222.20,
                "-2.222,20": -2222.20,
                "-11.111,20": -11111.20,
            };
    

    Suggestions welcome.

    0 讨论(0)
  • 2020-12-03 04:52

    The perfect solution

    accounting.js is a tiny JavaScript library for number, money and currency formatting.

    Check this for ref

    0 讨论(0)
  • 2020-12-03 04:55

    try this...

    var withComma = "23,3";
    var withFloat = "23.3";
    
    var compareValue = function(str){
      var fixed = parseFloat(str.replace(',','.'))
      if(fixed > 0){
          console.log(true)
        }else{
          console.log(false);
      }
    }
    compareValue(withComma);
    compareValue(withFloat);
    
    0 讨论(0)
  • 2020-12-03 04:56

    Do a replace first:

    parseFloat(str.replace(',','.').replace(' ',''))
    
    0 讨论(0)
  • 2020-12-03 05:04

    Here's a self-sufficient JS function that solves this (and other) problems for most European/US locales (primarily between US/German/Swedish number chunking and formatting ... as in the OP). I think it's an improvement on (and inspired by) Slawa's solution, and has no dependencies.

    function realParseFloat(s)
    {
        s = s.replace(/[^\d,.-]/g, ''); // strip everything except numbers, dots, commas and negative sign
        if (navigator.language.substring(0, 2) !== "de" && /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(s)) // if not in German locale and matches #,###.######
        {
            s = s.replace(/,/g, ''); // strip out commas
            return parseFloat(s); // convert to number
        }
        else if (/^-?(?:\d+|\d{1,3}(?:\.\d{3})+)(?:,\d+)?$/.test(s)) // either in German locale or not match #,###.###### and now matches #.###,########
        {
            s = s.replace(/\./g, ''); // strip out dots
            s = s.replace(/,/g, '.'); // replace comma with dot
            return parseFloat(s);
        }
        else // try #,###.###### anyway
        {
            s = s.replace(/,/g, ''); // strip out commas
            return parseFloat(s); // convert to number
        }
    }
    
    0 讨论(0)
提交回复
热议问题