加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

VB6非Banker舍入的Round函数

发布时间:2020-12-17 08:05:27 所属栏目:百科 来源:网络整理
导读:今天无意撞到一个帖子,发现争论蛮有意思的,依旧是关于 Round 函数:VB6的函数使用的是Banker舍入,很多时候我们实际可能需要的是常见的四舍五入,这里提供下面的函数去实现: Public Function StdRound(ByVal vPara As Variant,ByVal nDecimalBit As Long)

今天无意撞到一个帖子,发现争论蛮有意思的,依旧是关于 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读