|
虽然是坟贴,但是我看有人在讨论我也点进来看了一下,我确实没看明白楼主这个函数是什么意思。按照4舍6入5逢双进1的规则得到的结果为什么让人无法理解。
下面以保留2位小数为例:
22.525,结果应该是22.53
22.575,结果应该是22.57
Function Round2(number As Range, Optional num_digits As Integer = 0)
Dim Nums#, Numstep#, i%, k%, v As Boolean
If num_digits >= Len(Split(number, ".")(1)) Then Round2 = number: Exit Function
Numstep = 1: For i = 1 To num_digits
Numstep = Numstep / 10
Next i
i = InStr(1, number, ".") + num_digits
Select Case Mid(number, i + 1, 1)
Case Is > 5: v = True
Case 5: v = Mid(number, i, 1) Mod 2 = 0
End Select
Round2 = IIf(v, Val(Left(number, i)) + Numstep, Val(Left(number, i)))
End Function |
|