Rounding up to the nearest 0.05 in JavaScript
javascript, rounding
Solution
Multiply by 20, then divide by 20:
(Math.ceil(number*20)/20).toFixed(2)
Problem
Question Does anyone know of a way to round a float to the nearest 0.05 in JavaScript? Example ``` BEFORE | AFTER 2.51 | 2.55 2.50 | 2.50 2.56 | 2.60 ``` Current Code ``` var _ceil = Math.ceil; Math.ceil = function(number, decimals){ if (arguments.length == 1) return _ceil(number); multiplier = Math.pow(10, decimals); return _ceil(number * multiplier) / multiplier; } ``` Then elsewhere... `return (Math.ceil((amount - 0.05), 1) + 0.05).toFixed(2);` Which is resulting in... ``` BEFORE | AFTER 2.51 | 2.55 2.50 | 2.55 2.56 | 2.65 ```