[removed] rounding a float UP to nearest .25 (or whatever…)

后端 未结 4 1552
广开言路
广开言路 2021-01-21 03:39

All answers I can find are rounding to nearest, not up to the value... for example

1.0005 => 1.25  (not 1.00)
1.9   => 2.00
2.10 => 2.25
2.59 => 2.75         


        
4条回答
  •  面向向阳花
    2021-01-21 04:15

    Just multilpy by 4 and take the ceiled value with 4 times.

    var values = [1.0005, 1.9, 2.10, 2.59],
        round = values.map(function (a) { return Math.ceil(a * 4) / 4; });
    
    document.write('
    ' + JSON.stringify(round, 0, 4) + '
    ');

提交回复
热议问题