VBA函数返回错误的十进制值

时间:2018-01-03 00:32:19

标签: excel vba excel-vba

底部的两个函数都返回错误的值。

示例:对于142.22的员工部分,答案应为165.64,而不是VBA功能返回的165.39

同样,对于第二个函数,对于5048.94 - EmployerMPPportion,答案应该是50999.41,而答案是返回51000

我知道我使用了圆函数但它应该只舍入到2个小数点?

我刚刚在论坛上看到" BCD的空间效率不是很高,但在这种情况下你需要做出准确的权衡" 如果这是真的那么为什么Excel的内置函数总是返回正确的值?它们不是用VBA设计的吗?

Function MPPemployer (EmployeeMPPportion As Long)
    Dim c As Double
    c = Round((EmployeeMPPportion / (8.5 / 100) * (9.9 / 100)), 2)
    MPPemployer = c
End Function

Function Grossmppsalary (EmployerMPPportion As Long)
    Dim g As Double
    g = Round((EmployerMPPportion / (9.9 / 100)), 2)
    Grossmppsalary = g
End Function

1 个答案:

答案 0 :(得分:1)

您应该定义函数返回的内容,并将参数的Input类型更改为Double。对于原因的答案是,函数在计算之前将输入舍入为Long。

Function MPPemployer(EmployeeMPPportion As Double) As Double
    Dim c As Double

    c = Round((EmployeeMPPportion / (8.5 / 100) * (9.9 / 100)), 2)

    MPPemployer = c
End Function

Function Grossmppsalary(EmployerMPPportion As Double) As Double
    Dim g As Double

    g = Round((EmployerMPPportion / (9.9 / 100)), 2)

    Grossmppsalary = g
End Function