Pages

December 9, 2009

A Float is NOT a Currency

Do not use floats when representing currency.This is just asking for trouble.

Anytime you have to deal with floating point representations you can always get unpredictable results. I cannot count the number of times I have seen the "phantom penny" situations where balances are a penny off and you end up spending half a day with your accountant hat on trying to find where the penny went. You look and look and then you realize that somewhere in the code someone used a float inside the calculation. You change the variable type and your penny "magically" appears.

There are many alternatives in the modern programming languages for representing currency and they should be used. If those are not available, you can always go old school and use the int primitive type to represent currency.