VBA基础
Round函数四舍五入不正确,怎么办?
2004-11-23 22:34:42
作    者:Dave Mc Donald  发布日期:2003年9月10日 摘    要:Round函数返回一个数值,该数值是按照指定的小数位数进行四舍五入运算的结果。可是当保留位跟着的即使是5,有可能进位,也有可能舍去,机会各50%。这样就会造成在应用程序中计算有误。下面这个函数能真正实现四舍五入功能,用以取代Round函数。正    文:
Public Function RoundToLarger(dblInput As Double, intDecimals As Integer) As Double        '执行Round()函数,有可能进位    '也有可能舍去        

    Dim strFormatString As String '格式化字符串        '如果是“0”,则返回“0”,否则进行适当的格式化:    If dblInput <> 0 Then        strFormatString = "#." & String(intDecimals, "#")        RoundToLarger = Format(dblInput, strFormatString)    Else        RoundToLarger = 0    End If    End Function