|
建筑试验室要求对计算的数据进行“四舍六入五单双”的处理,就与平常用的四舍五入雷同,在网友的代码上略加更改而成,发给大家参考:
Public Function MyNub(X As Double, M As Integer) As Double
'M为定义小数位数
'示例:MyNub(5.25,1) 返回5.2
Dim I As Double
Dim J As Double
Dim Nub As Double
Dim vArr As Variant
Nub = X * 10 ^ M
If InStr(1, Nub, ".") > 0 Then
vArr = Split(Nub, ".")
I = vArr(0)
J = vArr(1) / (10 ^ Len(vArr(1)))
If J > 0.5 Then
I = I + 1
End If
If J = 0.5 Then
If Right(I, 1) Mod 2 > 0 Then
I = I + 1
Else
I = I
End If
End If
Else
I = Nub
End If
I = I / 10 ^ M
MyNub = I
End Function
学习中,请大家多提宝贵意见! |
|