简体   繁体   English

就像Java中的excel Mround函数一样

[英]Like excel Mround function in 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. 我正在尝试将小数四舍五入为最接近的小数,在Javascript中以0或5结尾。例如,如果值是543.55应该返回545,如果值是541.99应该返回540。我尝试使用Math.round()但不能实现。 kindly suggest me how to achieve that. 请为我提出建议。 Thanks in advance, Bijay 在此先感谢,Bijay

Math.round does the job with a little more extra work. Math.round可以完成一些额外的工作。 Divide by 5, round it, multiply it back by 5. It is a method I used a quite a bit in the past. 除以5,将其四舍五入,再乘以5。这是我过去使用过很多的方法。 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)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM