Rounding up to the nearest 0.05 in JavaScript

前端 未结 7 1984
你的背包
你的背包 2020-12-17 10:19

Question
Does anyone know of a way to round a float to the nearest 0.05 in JavaScript?

Example

BEFORE | AFTER         


        
7条回答
  •  误落风尘
    2020-12-17 10:48

    Rob's answer with my addition:

    (Math.ceil(number*20 - 0.5)/20).toFixed(2)
    

    Otherwise it always rounds up to the nearest 0.05.

    ** UPDATE **

    Sorry has been pointed out this is not what the orig poster wanted.

提交回复
热议问题