Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Like excel Mround function in Javascript

Tags:

javascript

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

like image 772
user1906222 Avatar asked Feb 28 '26 23:02

user1906222


1 Answers

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));
like image 130
Ozan Avatar answered Mar 02 '26 13:03

Ozan