I am trying to round a decimal number to nearest fraction number which end with 0 or 5 in Javascript.example if value is 543.55 should return 545 and if value is 541.99 should return 540. i tried with Math.round() but not able to achieve. kindly suggest me how to achieve that. Thanks in advance, Bijay
Math.round does the job with a little more extra work. Divide by 5, round it, multiply it back by 5. It is a method I used quite a bit in the past. Here is a simple snippet to show it is working.
function RoundTo(number, roundto){
return roundto * Math.round(number/roundto);
}
alert(RoundTo(543.55, 5));
alert(RoundTo(541.99, 5));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With