JavaScript - Keep trailing zeroes [duplicate]

十年热恋 提交于 2019-12-17 20:03:11

问题


I want to parse a string and I used parseFloat(), but it removes all the trailing zeroes. How to prevent this - I need to parse the string exactly - if I have 2.5000, I need exactly the same result as a floating-point number - 2.5000.


回答1:


You can do

parseFloat(2.5).toFixed(4);

If you need exactly the same floating point you may have to figure out the amount

var string = '2.54355';
parseFloat(string).toFixed(string.split('.')[1].length);

But i don't really understand why you even need to use parseFloat then? Numbers in javascript do not retain the floating-point count. so you would have to keep them as strings, and calculate against them as floats.



来源:https://stackoverflow.com/questions/24889498/javascript-keep-trailing-zeroes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!