How do I stop parseFloat() from stripping zeroes to right of decimal

后端 未结 5 979
长情又很酷
长情又很酷 2020-12-05 14:12

I have a function that I\'m using to remove unwanted characters (defined as currency symbols) from strings then return the value as a number. When returning the value, I am

相关标签:
5条回答
  • 2020-12-05 14:36

    this should work:

    return parseFloat(x).toFixed(2);
    

    you can test it by running this in firebug:

    var x = '0.00';
    alert(parseFloat(x).toFixed(2));
    
    0 讨论(0)
  • 2020-12-05 14:39

    parseFloat() turns a string into a floating point number. This is a binary value, not a decimal representation, so the concept of the number of zeros to the right of the decimal point doesn't even apply; it all depends on how it is formatted back into a string. Regarding toFixed, I'd suggest converting the floating point number to a Number:

    new Number(parseFloat(x)).toFixed(2);
    
    0 讨论(0)
  • 2020-12-05 14:42

    Here is dynamic version of floatParser for those who need

    function customParseFloat(number){
      if(isNaN(parseFloat(number)) === false){
        let toFixedLength = 0;
        let str = String(number);
    
        // You may add/remove seperator according to your needs
        [".", ","].forEach(seperator=>{
          let arr = str.split(seperator);
          if( arr.length === 2 ){
            toFixedLength = arr[1].length;
          }
        })
    
        return parseFloat(str).toFixed(toFixedLength);
      }
    
      return number; // Not a number, so you may throw exception or return number itself
    }
    
    0 讨论(0)
  • 2020-12-05 14:48

    simple:

    function decimalPlaces(float,length) {
      ret = "";
      str = float.toString();
      array = str.split(".");
      if(array.length==2) {
         ret += array[0] + ".";
         for(i=0;i<length;i++) {
            if(i>=array[1].length) ret += '0';
            else ret+= array[1][i];
         }
      }
      else if(array.length == 1) {
        ret += array[0] + ".";
        for(i=0;i<length;i++) {
           ret += '0'
        }
      }
    
      return ret;
    }
    
    document.write(decimalPlaces(3.123,6));
    
    0 讨论(0)
  • 2020-12-05 14:56

    For future readers, I had this issue as I wanted to parse the onChange value of a textField into a float, so as the user typed I could update my model.

    The problem was with the decimal place and values such as 12.120 would be parsed as 12.12 so the user could never enter a value like 12.1201.

    The way I solved it was to check to see if the STRING value contained a decimal place and then split the string at that decimal and then count the number of characters after the place and then format the float with that specific number of places.

    To illustrate:

    const hasDecimal = event.target.value.includes(".");
    const decimalValue = (hasDecimal ? event.target.value.split(".") : [event.target.value, ""])[1];
    const parsed = parseFloat(event.target.value).toFixed(decimalValue.length);
    const value = isNaN(parsed) ? "" : parsed;
    onEditValue(value);
    
    0 讨论(0)
提交回复
热议问题