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

后端 未结 11 757
时光取名叫无心
时光取名叫无心 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: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);
    

提交回复
热议问题