brb
brb

Reputation: 1179

Precision issues between Excel + VBA with currency formats

I have a user defined function (UDF) which takes two inputs. Both inputs have been declared as type Double. However, while the input has a large number of decimal places, once passed to the UDF, the number of decimal places gets truncated.

Curiously, I cannot seem to reproduce this behaviour in a clean workbook (ie, I wonder if there is something going on with this particular workbook...).

The (simplied) UDF is below.

Function Wage(HourlyWage As Double, HoursWorked As Double) As Double

    Wage = HourlyWage

End Function

as well as a screenshot of what is happening.

enter image description here

The formula bar shows the UDF call. It is taking columns C and D as inputs. HourlyWage is 25.30364372+ in column C, but once passed to the UDF Wage, the HourlyWage is truncated to 25.30360000 despite being declared as type Double.

I understand general points about floating point precision, but this surely applies equally to the input in column C as it does to the UDF (indeed, column C is in actual fact the output of another UDF which has been declared as type Double).

Why can't decimal numbers be represented exactly in binary?

Why are floating point numbers inaccurate?

Additional information about the EXCEL workbook.

Any ideas on how to fix this? The loss of precision is causing me issues...

Upvotes: 0

Views: 822

Answers (1)

brb
brb

Reputation: 1179

This answer is based on comments from barrowc and Peh above. Thank you both for your help.

Currency data types have a maximum of 4 decimal points (https://learn.microsoft.com/en-gb/office/vba/language/reference/user-interface-help/currency-data-type).

Column C has been formatted with a custom currency format. When fed to the VBA UDF function, decimal points beyond the 4th decimal place are truncated despite the variable being declared as type Double inside the UDF.

Changing the number format solves the problem as per screenshot below.

enter image description here

This can be verified using Debug.Print

Sub brb()
    Debug.Print [C6], [E6]
    Debug.Print [C8], [E8]
End Sub

enter image description here

Upvotes: 2

Related Questions