Using Math.round in javascript adds weird number of 0s at the end [duplicate]

☆樱花仙子☆ 提交于 2019-12-25 18:23:56

问题


Possible Duplicate:
Is JavaScript’s Math broken?

I'm using Math.round to round the number and for some reason it adds weird number of 0s at the end which should not be there. Here is my code:

return Math.round($digit * 1000) / 1000; 

i want numbers to have 3 decimal points

example:

  Math.round(29.469 * 1000) / 1000 = returns this value: 29.469000000000023 

cant seem to figure out why. is there a different way of rounding decimals to certain decimal point? i need 3 decimal points. Or maybe trim everything after third decimal digit? That would be perfect take the returned number and trim everything after third decimal.


回答1:


yourVariable.toFixed(3);

Will give you what you want.

MDN docs



来源:https://stackoverflow.com/questions/12918761/using-math-round-in-javascript-adds-weird-number-of-0s-at-the-end

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