解决access中round函数不能正确四舍五入的问题 (备忘)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ChoasRules/article/details/6738074
Function round(x As Double, Y As Integer) As Double
'四舍五入函数
'将数字转化为字符串,判断是否大于4,截掉多余部分
Dim douShuZhi As Double

Dim strShuZhi As String
Dim b As String
Dim tt As Integer
Dim strZiFu As String
Dim longshuzhi As Long

'当数据以科学计数法表示时,换成以下格式
x = Format(x, "0.00000000")
strShuZhi = str(x)
b = strShuZhi

tt = 1

'判断小数点在字符串中的位置
Do While Not Left(b, 1) = "." And Len(b) <> 0
    b = Right(b, Len(b) - 1)
    tt = tt + 1
 Loop
 
'取出需要判断数字
If Len(strShuZhi) <> 0 Then
strZiFu = Mid(strShuZhi, Y + tt + 1, 1)
Else
    round = x
    Exit Function
    
End If

'判断是否大于4
'区分是否正负数————Sgn(X)
If Val(strZiFu) > 4 Then
    round = Val(Left(strShuZhi, tt + Y)) + Sgn(x) * (1 / (10 ^ Y))
Else
    round = Val(Left(strShuZhi, tt + Y))
End If

End Function

展开阅读全文

没有更多推荐了,返回首页