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