题
鉴于号码,如499,73433,2348可以使用哪些VBA到舍入到最近5或10?或任意数量?
按照5:
499 -> 500
2348 -> 2350
7343 -> 7345
按照10:
499 -> 500
2348 -> 2350
7343 -> 7340
等
解决方案 2
<强>集成应答强>
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使用银行家舍入时,到最接近的偶数,如果你不知道它可以是惊人的:
msgbox round(1.5) 'result to 2
msgbox round(2.5) 'yes, result to 2 too
谢谢大家。的
其他提示
这是简单的数学。给定一个号码X和舍入因子N,公式将是:
轮(X / N)* N
要舍入到最接近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(“N 2”)的结果舍入远离零,得到“10.67”。无需额外的参数math.round返回10.66,这可能导致不必要的差异。
“实施例:499至最接近5.你将使用ROUND()函数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)
只需ROUND(X / 5)* 5应做的工作。
,所以我将使用此我不能添加评论
在一个VBS运行和乐趣搞清楚为什么2给出的2结果
你不能信任圆
msgbox round(1.5) 'result to 2
msgbox round(2.5) 'yes, result to 2 too
类似的东西?
'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
尝试该功能
-------------- ----------------启动
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年“下,同时避免躺在陈述的” 是存活超过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中的工作方式,你只需要使用: WorksheetFunction.Round(数字,小数)
此方式在银行或计费的舍入不这样做的舍入。