Floating point with currency

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
User avatar
Slippy
Forum Contributor
Posts: 113
Joined: Sat Jul 12, 2003 11:31 pm
Location: Vancouver eh!

Floating point with currency

Post 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...
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post 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.
User avatar
Slippy
Forum Contributor
Posts: 113
Joined: Sat Jul 12, 2003 11:31 pm
Location: Vancouver eh!

Post 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.
Post Reply