Javascript : Rounding 2 decimal places of boundary 0.5

China☆狼群 提交于 2020-01-30 12:03:42

问题


How to get the rounding number of 0.5, previously I found in this link:

Click here

But this is not what I want:

I want like this example:

  1. 10.24 = 10.25
  2. 17.90 = 17.90
  3. 3.89 = 3.90
  4. 7.63 = 7.65

jQuery will be fine with me.


回答1:


Use Math.round(num * 20) / 20, if you are looking to round a number to nearest 0.05.




回答2:


This will do what you need...

function roundoff(value) {
    return (Math.round(value / 0.05) * 0.05).toFixed(2);
}

Using Math.round will round the value to the nearest 0.05, and then using toFixed(2) will ensure the result has 2 decimal places, even if they're both zeros.

Here's a working example...

http://jsfiddle.net/xoo7c898/



来源:https://stackoverflow.com/questions/31139678/javascript-rounding-2-decimal-places-of-boundary-0-5

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