Page 1 of 1
Floating point with currency
Posted: Thu Apr 15, 2004 4:01 pm
by Slippy
I read somewhere in the manual that one shouldn't use Float variables when dealing with Currency... It seems to work fine for me though... just use the round function properly when you do a multiplication/division operation. So far everything adds up.
Does anybody know what to use other than float
Just curious...
Posted: Thu Apr 15, 2004 4:19 pm
by Bill H
If you are using a MySQL database the "decimal" type is a godsend.
For general calculations the float type is dangerous. It can be made safe if you use the round() function as you say, but you must be sure to use it a lot, including before and after calculations. The inexactness of the float can bite off a penny in a heartbeat.
I would suggest making yourself a set of functions for dealing with currency. I have a set of them I use in C language that puts the dollars and cents into integers and performs the calculations on integers. A similar set in PHP using round() would be easier to construct.
Posted: Thu Apr 15, 2004 4:29 pm
by Slippy
That's a good idea.. I wonder if someone has a class lying around (hmm...) wouldn't want to re-invent the wheel.
I was looking at the php stuff for currency, but my understanding was that I had to set the locale (sp?) -- so that it knew what format the money was in... kind of strange.