奇怪呀,我 在 vb 中用 Round 函数四舍五入时,又时正确而又时错误呀!!!
举例如下: MsgBox Round(652.45, 1) ''得到652.4 错误,应为652.5
MsgBox Round(652.46, 1) ''得到652.5 正确
MsgBox Round(652.05, 1) ''得到652.0 错误,应为652.1
MsgBox Round(652.06, 1) ''得到652.1 正确
MsgBox Round(652.55, 1) ''得到652.6 正确怎么回师?有替代 Round 函数的办法吗?
要四舍五入用 formatnumber
我不会用 formatnumber,还是用比较老的办法,保留一位小数Int(Press*10 + 0.5) / 10再加上格式要求
Format(Int(Press *10 + 0.5) / 10, "0.0")
Format(652.05, "0.0") 结果= 652.1
谢谢fa7274(聂风)、dyb1234567(蚂蚁)!谢谢两位的答复
我用自定义函数完成的!供需要这参考,如下:Function FormatData1(D As Variant, K As Integer) As String
D1 = Val(D) ''转为数值
D1 = Abs(D1) / K + 0.05 ''先除 K取得平均值,再在小数第二位加 5
D1 = Int(Val(D1 * 10)) / 10 ''先乘10取整数部分,即将第1 位小数保留
If Val(D) < 0 Then D1 = 0 - D1 ''为负数时
FormatData1 = Format(D1, "0.0") ''以小数位一位显示
End FunctionDim po For i = 1 To N
Po = Po + Val(.TextMatrix(i, 1))
.......
Next i
Po = FormatData1(Po, N) '' po 是合计,N 是合计包含的元素个数即除数
Dim x As Double '处理变量
x = 4567.15
x = x / 7 '执行除 运算
Debug.Print x '结果:652.45
Debug.Print Format(x, "#0.0") '结果:652.5
End Sub