Like excel Mround function in Javascript - Stack Overflow

I am trying to round a decimal number to nearest fraction number which end with 0 or 5 in Javascript.ex

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

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

Share Improve this question asked Jan 17, 2015 at 18:54 user1906222user1906222 776 silver badges12 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 14

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));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743781208a4505832.html

相关推荐

  • Like excel Mround function in Javascript - Stack Overflow

    I am trying to round a decimal number to nearest fraction number which end with 0 or 5 in Javascript.ex

    7天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信