如何向下舍入到最接近的 X 数字 - VBScript 的伪代码

发布于 2024-09-14 09:24:48 字数 293 浏览 2 评论 0原文

我正在尝试将数字向下舍入到最接近的 15、20、30。即

726 到最接近的 30 是 700

714 到最接近的 15 是 700 等等

VBScript 代码会非常有帮助,但伪代码也会有很大的帮助!

编辑:抱歉,我忘了说,726实际上是一个用int表示的时间,即07:26。所以这应该是 07:00,而不是 690

再次编辑:我只是提取分钟并使用人们回答的代码。希望这对其他人也有帮助。谢谢!

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傲世九天 2024-09-21 09:24:48

伪代码:

diff = num mod nearest
return num - diff

所以 726 mod 30 = 6

726 - 6 = 720

vbscript:

Function GetNearest(num, nearest)
    Dim diff = num mod nearest
    GetNearest = num - diff
End Function

Pseudo 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
和我恋爱吧 2024-09-21 09:24:48

您在标签中列出了多种语言。我将使用 C#,但是更通用的算法:

int n = 726;
int q = 30;
int r = Math.Floor(n / q) * q;

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;
把昨日还给我 2024-09-21 09:24:48

另一种方法是使用整数除法:726 / 30 * 30 = 720

another way to do it is just to use integer division: 726 / 30 * 30 = 720

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文