How do I get floating point numbers to work like currency?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jtonsfeldt
Forum Newbie
Posts: 3
Joined: Mon Nov 01, 2004 2:30 pm

How do I get floating point numbers to work like currency?

Post by jtonsfeldt »

Hi,

I'm working on a shopping site which takes prices, quantities, etc. and calculates a final cost. I cannot figure out how to get floating point numbers to automatically look like currency, ie. 43.50 as opposed to 43.5. I've spent quite a bit of time researching to no avail, so now I'm turning to the forum. Can anyone help? Hope this can be of use to others out there, too.

Thanks.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

look at number_format() you should.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Re: How do I get floating point numbers to work like currenc

Post by Roja »

jtonsfeldt wrote:I cannot figure out how to get floating point numbers to automatically look like currency, ie. 43.50 as opposed to 43.5.
*LOOKING* like currency is easy. Use number_format as Burrito suggested.

However, the reason it LOOKS different is because it *is* different. You are probably storing them as a float. Floats don't guarantee precision, in fact, just the opposite. For currency, you should be using NUMBER or DECIMAL, where the values are stored literally and with high-precision.
Post Reply