给定 499、73433、2348 等数字,我可以使用什么 VBA 舍入到最接近的 5 或 10?或任意数字?
5:
499 -> 500
2348 -> 2350
7343 -> 7345
到 10 点:
499 -> 500
2348 -> 2350
7343 -> 7340
等等
这是简单的数学。给定一个数字 X 和一个舍入因子 N,公式将是:
圆形(X / N)*N
综合答案
X = 1234 'number to round
N = 5 'rounding factor
round(X/N)*N 'result is 1235
对于浮点到整数,1234.564 到 1235,(这是 VB 特有的,大多数其他语言只是截断)做:
int(1234.564) 'result is 1235
当心: VB 使用Bankers Rounding,到最接近的偶数,如果你不知道这可能会令人惊讶:
msgbox round(1.5) 'result to 2
msgbox round(2.5) 'yes, result to 2 too
谢谢大家。
四舍五入到最接近的 X(不是 VBA 特定的)
N = X * int(N / X + 0.5)
其中 int(...) 返回下一个最小的整数。
如果您可用的舍入函数已经舍入到最接近的整数,则省略 0.5
在 VB 中,math.round 有额外的参数来指定小数位数和舍入方法。 Math.Round(10.665, 2, MidpointRounding.AwayFromZero)将返回 10.67 。如果数字是小数或单一数据类型,则 math.round 返回小数数据类型。如果是双精度,则返回双精度数据类型。如果选项严格打开,这可能很重要。
(10.665).ToString("n2") 的结果从零四舍五入得到“10.67”。没有额外的参数 math.round 返回 10.66,这可能会导致不必要的差异。
'示例:将 499 舍入到最接近的 5。您将使用 ROUND() 函数。
a = inputbox("number to be rounded")
b = inputbox("Round to nearest _______ ")
strc = Round(A/B)
strd = strc*B
msgbox( a & ", Rounded to the nearest " & b & ", is" & vbnewline & strd)
对于严格的 Visual Basic 方法,您可以将浮点值转换为整数以四舍五入到所述整数。VB 是一种罕见的类型转换语言(大多数其他语言只是截断。)
5 或 x 的倍数可以简单地通过轮前除和轮后相乘来完成。
如果你想四舍五入并保留小数位,Math.round(n, d) 可以工作。
这是我们的解决方案:
Public Enum RoundingDirection
Nearest
Up
Down
End Enum
Public Shared Function GetRoundedNumber(ByVal number As Decimal, ByVal multiplier As Decimal, ByVal direction As RoundingDirection) As Decimal
Dim nearestValue As Decimal = (CInt(number / multiplier) * multiplier)
Select Case direction
Case RoundingDirection.Nearest
Return nearestValue
Case RoundingDirection.Up
If nearestValue >= number Then
Return nearestValue
Else
Return nearestValue + multiplier
End If
Case RoundingDirection.Down
If nearestValue <= number Then
Return nearestValue
Else
Return nearestValue - multiplier
End If
End Select
End Function
用法:
dim decTotal as Decimal = GetRoundedNumber(CDec(499), CDec(0.05), RoundingDirection.Up)
类似的东西?
'nearest
n = 5
'n = 10
'value
v = 496
'v = 499
'v = 2348
'v = 7343
'mod
m = (v \ n) * n
'diff between mod and the val
i = v-m
if i >= (n/2) then
msgbox m+n
else
msgbox m
end if
只需 ROUND(x/5)*5 就可以完成这项工作。
我无法添加评论,所以我将使用它
在 vbs 中运行它,并找出为什么 2 给出 2 的结果很有趣
你不能相信回合
msgbox round(1.5) 'result to 2
msgbox round(2.5) 'yes, result to 2 too
试试这个功能
- - - - - - - 开始 - - - - - - - -
Function Round_Up(ByVal d As Double) As Integer
Dim result As Integer
result = Math.Round(d)
If result >= d Then
Round_Up = result
Else
Round_Up = result + 1
End If
End Function
- - - - - - -结尾 - - - - - -
我稍微更新了“社区维基”(最佳答案)提供的功能,只是四舍五入到最接近的 5(或您喜欢的任何值),但有一个例外:四舍五入的数字永远不会优于原始数字。
这在需要说“一家公司存活 47 年”的情况下很有用:我希望网页显示“存活超过 45 年”,同时避免说谎说“存活超过 45 年” 50 年”。
所以当你给这个函数输入 47 时,它不会返回 50,而是会返回 45。
'Rounds a number to the nearest unit, never exceeding the actual value
function RoundToNearestOrBelow(num, r)
'@param num Long/Integer/Double The number to be rounded
'@param r Long The rounding value
'@return OUT Long The rounded value
'Example usage :
' Round 47 to the nearest 5 : it will return 45
' Response.Write RoundToNearestBelow(47, 5)
Dim OUT : OUT = num
Dim rounded : rounded = Round((((num)) / r), 0) * r
if (rounded =< num) then
OUT = rounded
else
OUT = rounded - r
end if
'Return
RoundToNearestOrBelow = OUT
end function 'RoundToNearestOrBelow
要在 Visual Basic 中模仿 Excel 中的 round 函数的工作方式,您只需使用: WorksheetFunction.Round(number, decimals)
这样,银行或会计四舍五入就不会进行四舍五入。