菱歌慢慢 |
2009-06-30 13:04 |
vb四舍五入异常的解决之道
大致情况如下: round (8.75*,1)=8.8
round (7.25*,1)=7.2 正常应为7.3
round (6.75*,1)=6.8 round (5.25*,1)=5.2 正常应为5.3
百思不得其解,原来这就所谓的bug
无奈采取如下措施 format(round(7.25*,2),"0.0")=7.3 输出的需要
http://blog.sina.com.cn/s/blog_5dee7be30100cmr5.html 以下为转帖内容 摘自雪国的博客
vb 中round函数 四舍六入逢五奇进偶不进的函数(2009-03-01 21:34:51)标签:vba 函数 round函数 四舍六入 it 分类:IT
前几天开发小工具时,发现的vb 中round函数原来不是四舍五入。
而是水文等常用的四舍六入五单双。差点用错它。所以写出来跟大家分享。
函数 Round 和 VBA Round 返回各不同 函数的 Round() 使用 : 四舍五入 (Arithmetic rounding)
VBA的 Round() 使用 : 四舍六入五单双 (Banker's rounding)
四舍六入五单双, 数值修约的方法 : 四舍六入五考虑 五后非零应进一 五后皆零视奇偶 五前为偶应舍去 五前为奇则进一
函数 Round 和 VBA Round 的测试 :
1] 测试数值
A列 1.535 1.545 1.555 1.565
2] 测试公式
2.1] 函数 ROUND 的公式为 :
=ROUND(A1,2)
2.2] VBA ROUND 的公式为, 自定义函数 :
=VBAROUND(A1,2)
自定义函数 :
Function VBAROUND(Number As Double, Decimals As Long) As Double VBAROUND = Round(Number, Decimals) End Function
3] 测试返回结果
测试数值………………..函数 Round…….…..VBA Round 1.535………………………..1.54………………..1.54 1.545………………………..1.55………………..1.54 1.555………………………..1.56………………..1.56 1.565………………………..1.57………………..1.56 本贴来自ZDNetChina中文社区 http://bbs.zdnet.com.cn ,本贴地址:http://bbs.zdnet.com.cn/viewthread.php?tid=196591
如果要四舍五入的话可以调用工作表函数 sheet1.cells(1,1)=application.round(1228.5,0)
记住 ,调用工作表函数必须在前面加上application.
顺便查了一下,四舍六入五留双的英文说法。
If the number after point is less than five please treat as zero;if the number after point is more than five please treat as one;and when it just five you can stay it up and waiting for another one then to treat as one.
阿果石油网旗下站点:石油文库 | 石油资讯 |石油英才 | 石油供求 | 石油搜索
|
|