将数字四舍五入到最接近的 5 或 10 或 X
给定数字如 499、73433、2348,我可以使用什么 VBA 来四舍五入到最接近的 5 或 10? 或任意数字?
5:
499 -> 500
2348 -> 2350
7343 -> 7345
10:
499 -> 500
2348 -> 2350
7343 -> 7340
等
Given numbers like 499, 73433, 2348 what VBA can I use to round to the nearest 5 or 10? or an arbitrary number?
By 5:
499 -> 500
2348 -> 2350
7343 -> 7345
By 10:
499 -> 500
2348 -> 2350
7343 -> 7340
etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
对于严格的 Visual Basic 方法,您可以将浮点值转换为整数,以四舍五入为所述整数。 VB 是在类型转换上进行舍入的罕见语言之一(大多数其他语言只是截断)。5
或 x 的倍数可以简单地通过在舍入之前除法和在舍入之后相乘来完成。
如果你想四舍五入并保留小数位,Math.round(n, d) 就可以了。
For a strict Visual Basic approach, you can convert the floating-point value to an integer to round to said integer. VB is one of the rare languages that rounds on type conversion (most others simply truncate.)
Multiples of 5 or x can be done simply by dividing before and multiplying after the round.
If you want to round and keep decimal places, Math.round(n, d) would work.
这是我们的解决方案:
用法:
Here is our solution:
Usage:
只需 ROUND(x/5)*5 就可以完成这项工作。
Simply ROUND(x/5)*5 should do the job.
我无法添加评论,所以我将在 vbs 运行中使用它
,并有趣地弄清楚为什么 2 给出的结果是 2
你不能信任回合
I cannot add comment so I will use this
in a vbs run that and have fun figuring out why the 2 give a result of 2
you can't trust round
类似的东西?
something like that?
试试这个功能
--------------开始----------------
-------------结束-- ----------
Try this function
--------------start----------------
-------------end ------------
我稍微更新了“社区维基”(最佳答案)提供的函数,只是四舍五入到最接近的 5(或任何你喜欢的值),但有一个例外:四舍五入的数字永远不会优于原始数字。
当需要说“一家公司已存在 47 年”时,这非常有用:我希望网页显示“已存在超过 45 年” strong>,同时避免撒谎说“已经活了 50 多年”。
因此,当您向此函数输入 47 时,它不会返回 50,而是返回 45。
I slightly updated the function provided by the "community wiki" (the best answer), just to round to the nearest 5 (or anything you like), with this exception : the rounded number will NEVER be superior to the original number.
This is useful in cases when it is needed to say that "a company is alive for 47 years" : I want the web page to display "is alive for more than 45 years", while avoiding lying in stating "is alive for more than 50 years".
So when you feed this function with 47, it will not return 50, but will return 45 instead.
要在 Visual Basic 中模仿舍入函数在 Excel 中的工作方式,您只需使用:
WorksheetFunction.Round(number,decimals)
这样银行或会计舍入就不会进行舍入。
To mimic in Visual Basic the way the round function works in Excel, you just have to use:
WorksheetFunction.Round(number, decimals)
This way the banking or accounting rounding don't do the rounding.
这是简单的数学。 给定数字 X 和舍入因子 N,公式为:
round(X / N)*N
It's simple math. Given a number X and a rounding factor N, the formula would be:
round(X / N)*N
综合答案
对于浮点到整数,1234.564到1235,(这是VB特定的,大多数其他语言只是截断):
注意: VB使用银行家四舍五入,到最接近的偶数,如果您不知道的话,这可能会令人惊讶:
谢谢大家.
Integrated Answer
For floating point to integer, 1234.564 to 1235, (this is VB specific, most other languages simply truncate) do:
Beware: VB uses Bankers Rounding, to the nearest even number, which can be surprising if you're not aware of it:
Thank you everyone.
舍入到最接近的 X(不是 VBA 特定的)
N = X * int(N / X + 0.5)
其中 int(...) 返回下一个最小的整数。
如果您可用的舍入函数已经四舍五入到最近整数,则省略 0.5 的加法
To round to the nearest X (without being VBA specific)
N = X * int(N / X + 0.5)
Where int(...) returns the next lowest whole number.
If your available rounding function already rounds to the nearest whole number then omit the addition of 0.5
在 VB 中,math.round 有附加参数来指定小数位数和舍入方法。 Math.Round(10.665, 2, MidpointRounding.AwayFromZero) 将返回 10.67 。 如果数字是小数或单一数据类型,则 math.round 返回小数数据类型。 如果是 double,则返回 double 数据类型。 如果打开了选项严格,这可能很重要。
(10.665).ToString("n2") 的结果从零舍入得到“10.67”。 如果没有额外的参数, math.round 返回 10.66,这可能会导致不必要的差异。
In VB, math.round has additional arguments to specify number of decimal places and rounding method. Math.Round(10.665, 2, MidpointRounding.AwayFromZero) will return 10.67 . If the number is a decimal or single data type, math.round returns a decimal data type. If it is double, it returns double data type. That might be important if option strict is on.
The result of (10.665).ToString("n2") rounds away from zero to give "10.67". without additional arguments math.round returns 10.66, which could lead to unwanted discrepancies.
'示例:将 499 舍入到最接近的 5。您将使用 ROUND() 函数。
'Example: Round 499 to nearest 5. You would use the ROUND() FUNCTION.