round to nearest .25 javascript

后端 未结 8 692
我在风中等你
我在风中等你 2020-12-08 14:07

I want to convert all numbers to the nearest .25

So...

5 becomes 5.00
2.25 becomes 2.25
4 becomes 4.00
3.5 becomes 3.50

Thanks

8条回答
  •  长情又很酷
    2020-12-08 14:55

    Here is @Gumbo's answer in a form of a function:

    var roundNearQtr = function(number) {
      return (Math.round(number * 4) / 4).toFixed(2);
    };
    

    You can now make calls:

    roundNearQtr(5.12345); // 5.00
    roundNearQtr(3.23); // 3.25
    roundNearQtr(3.13); // 3.25
    roundNearQtr(3.1247); // 3.00
    

提交回复
热议问题