admin管理员组文章数量:1335138
I'm trying to round down a number to the nearest say 15, 20, 30. ie
726 to the nearest 30 is 700
714 to the nearest 15 is 700 etc
VBScript code would be very helpful but pseudocode would also be a huge help!
EDIT: Sorry, I forgot to say, that 726 is really a time expressed as an int, ie 07:26. So this should be 07:00, not 690
EDIT Again: I'm just extracting the minute and using the code people have answered with. Hopefully this will help someone else too. Thanks!
Thanks
I'm trying to round down a number to the nearest say 15, 20, 30. ie
726 to the nearest 30 is 700
714 to the nearest 15 is 700 etc
VBScript code would be very helpful but pseudocode would also be a huge help!
EDIT: Sorry, I forgot to say, that 726 is really a time expressed as an int, ie 07:26. So this should be 07:00, not 690
EDIT Again: I'm just extracting the minute and using the code people have answered with. Hopefully this will help someone else too. Thanks!
Thanks
Share Improve this question edited Aug 16, 2010 at 16:27 Igor K asked Aug 16, 2010 at 16:08 Igor KIgor K 1,3713 gold badges13 silver badges19 bronze badges 4- I think you mean 726 (rounded down) to the nearest 30 is 720 and 714 (rounded down) to the nearest 15 is 705. – Daniel Renshaw Commented Aug 16, 2010 at 16:16
- Yes I forgot to mention that its really times, please see the question. Really sorry, my mistake – Igor K Commented Aug 16, 2010 at 16:23
- can you please further clarify how time like 5:45pm would appear in your int representation and if the last 2 digits are ever over 59... – Oded Commented Aug 16, 2010 at 16:26
- Thanks Oded, all sorted with your original code – Igor K Commented Aug 16, 2010 at 16:28
3 Answers
Reset to default 3Pseudo code:
diff = num mod nearest
return num - diff
So 726 mod 30 = 6
726 - 6 = 720
vbscript:
Function GetNearest(num, nearest)
Dim diff = num mod nearest
GetNearest = num - diff
End Function
You listed a bunch of languages in your tags. I'm going with C#, but a more generic algorithm:
int n = 726;
int q = 30;
int r = Math.Floor(n / q) * q;
another way to do it is just to use integer division: 726 / 30 * 30 = 720
本文标签: javascriptHow to round down to nearest X numberpseudocode for VBScriptStack Overflow
版权声明:本文标题:javascript - How to round down to nearest X number - pseudocode for VBScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742259101a2442178.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论