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.

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...