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

后端 未结 5 985
长情又很酷
长情又很酷 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: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);
    

提交回复
热议问题