将数字四舍五入到最接近的 5 或 10 或 X [英] Rounding a number to the nearest 5 or 10 or X

查看:34
本文介绍了将数字四舍五入到最接近的 5 或 10 或 X的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定像 499、73433、2348 这样的数字,我可以使用什么 VBA 来四舍五入到最接近的 5 或 10?还是任意数字?

Given numbers like 499, 73433, 2348 what VBA can I use to round to the nearest 5 or 10? or an arbitrary number?

5 点:

 499 ->  500
2348 -> 2350
7343 -> 7345

到 10 点:

 499 ->  500
2348 -> 2350
7343 -> 7340

推荐答案

综合答案

X = 1234 'number to round
N = 5    'rounding factor
round(X/N)*N   'result is 1235

对于浮点到整数,1234.564 到 1235,(这是 VB 特定的,大多数其他语言只是截断)做:

For floating point to integer, 1234.564 to 1235, (this is VB specific, most other languages simply truncate) do:

int(1234.564)   'result is 1235

注意: VB 使用 Bankers Rounding,到最接近的偶数,如果您不知道,这可能会令人惊讶:

Beware: VB uses Bankers Rounding, to the nearest even number, which can be surprising if you're not aware of it:

msgbox round(1.5) 'result to 2
msgbox round(2.5) 'yes, result to 2 too

谢谢大家.

这篇关于将数字四舍五入到最接近的 5 或 10 或 X的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆