VB6非Banker舍入的Round函数

今天无意撞到一个帖子,发现争论蛮有意思的,依旧是关于 Round 函数:VB6的函数使用的是Banker舍入,很多时候我们实际可能需要的是常见的四舍五入,这里提供下面的函数去实现:

Public Function StdRound(ByVal vPara As Variant, ByVal nDecimalBit As Long) As Variant
    On Error Resume Next
    If IsNumeric(vPara) Then
       StdRound = Int(Val((vPara * 10 ^ nDecimalBit) + 0.5)) / 10 ^ nDecimalBit
    End If
End Function

另外这里附带一个摘到的文章,看看就知道,实际上 Banker舍入在实际中使用误差会小那么点点的...... 大笑

"Banker's rounding" is not bad

some people think "Banker's rounding" is bad,but it is not the case. 
This "Banker's" method uses the Gauss rule that if you are in an perfect half case,
you must round to the nereast digit that can be divided by 2 (0,2,4,6,8). 
This rule is important to obtain more accurate results with rounded numbers after operation.

Now, an example :
             2 digits                2 digits
Unrounded    "Standard" rounding    "Gaussian" rounding
  54.1754      54.18                  54.18
 343.2050     343.21                 343.20
+106.2038    +106.20                +106.20 
=========    =======                =======
 503.5842     503.59                 503.58

Which one is nearer from unrounded result ? The "Gaussian" one (Difference of 0.0042 with "Gaussian/Banker" and 0.0058 with "Standard" rounding.)

Another example with half-round cases only:
             1 digit                1 digit
Unrounded    "Standard" Rounding    "Gaussian rounding"
  27.25        27.3                   27.2
  27.45        27.5                   27.4  
+ 27.55      + 27.6                 + 27.6
=======      ======                 ======  
  82.25        82.4                   82.2

Again, the "Gaussian" rounding result is nearer from the unrounded result than the "Standard" one.







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值