Page 1 of 1

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

Posted: Wed Jul 06, 2005 1:12 pm
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.

Posted: Wed Jul 06, 2005 1:14 pm
by Burrito
look at number_format() you should.

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

Posted: Wed Jul 06, 2005 2:27 pm
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.