四舍六入逢五奇进偶不进,零占位问题
四舍六入逢五奇进偶不进的函数,但是不能实现小数点前用零站位,如果是9.6能变成9.60用0补齐,.68变0.68,.07变为0.07保留两位小数.可以得用FORMAT但我不知道如何修改,基本运算都行,就是差零占位,希望大家都帮忙.Private Sub Command1_Click()
Print Rvt(0.075, 2)
End Sub
Function Rvt(ByVal x As Double, ByVal n As Integer) As Double
'四舍六入逢五奇进偶不进的函数
Const IFIX = 15
Dim sFmt As String
Dim sRet As String, sTmp As String
Dim intR As Integer, intRT As Integer
If n < 0 Then n = 0
sFmt = "0." & String(n + IFIX, "0")
sTmp = Format(x, sFmt)
If n = 0 Then
intR = CInt(Left(Right(sTmp, IFIX + 2), 1))
intRT = CInt(Left(Right(sTmp, n + IFIX), 1))
sRet = Left(sTmp, Len(sTmp) - n - IFIX - 1)
Else
intR = CInt(Left(Right(sTmp, n + IFIX), 1))
intRT = CInt(Left(Right(sTmp, n + IFIX - 1), 2))
sRet = Left(sTmp, Len(sTmp) - n - IFIX + 2)
End If
If intRT = 5 Then
If intR Mod 2 = 0 Then
Rvt = CDbl(sRet)
Else
Rvt = Round(x, n)
End If
Else
Rvt = Round(x, n)
End If
End Function