Stupid math error

This bugged me. It is my fault - not lucee - just want to post it in case it bites anyone else.

I am displaying financial information where I display results of a calculation.
Works most of the time, but sometimes the amount if incorrect.
A calculation that fails is:
5,000 - 1,377.68 - 3,622.32
Should equal $0 but I get -$4.54
The problem is I used val() around each value, which yields:
5000.0000 - 1377.6800 - 3622.3200 =
removing the val() from the result gives -4.54747350886E-013 which is basically 0, but val of that gives -4.54
The point is do not use val() on the result of a calculation

1 Like

val() is the Devil’s work. Should never use it for any financial stuff or in fact… anything. It is for one thing only:

Converts numeric characters that occur at the beginning of a string to a number. If conversion fails, returns zero.

Which is a very specific situation IMHO.

Consider using PrecisionEvaluate() for financial calculations.

See http://docs.lucee.org/reference/functions/precisionevaluate.html