tags:

views:

30

answers:

1

I want to store prices like this in the mysql database:

1000

instead of like this:

10.00

And when I echo the output I want the number to be displayed with decimals and commas in the proper places. So if this is in the database:

100000

it should display as:

1,000.00

if this is in the database:

1000000

it should show as

10,000.00

and if this is in the database:

100

it should show as:

1.00

How can this be achieved?

+5  A: 

Stored them as integers and divide by 100 when you retrieve them (and multiply by 100 when you store them). If you don't want to do that, you might want to consider storing them as DECIMAL

You can format it using the number_format function

echo number_format ($number/100, 2 , '.' , ',' );

Prints 1,000.00 for $number = 100000

NullUserException
If you do the division in MySQL, cast to DECIMAL or you'll just get integer math - no decimals.
OMG Ponies
I should add, just for completeness, that currency should *never* be stored as a `float` of any kind.
staticsan