Skip to main content
April 4, 2007
Question

Currency in PHP or MySQL

  • April 4, 2007
  • 2 replies
  • 532 views
I have made a database where I want to list certain items and their prices. In MySQL I had set my prices to be the type to be double. In my PHP when I type lets say 19.95 when I see it live on my PHP page it shows $19.95 which is great and what I want. However the problem lies when I want to list something that has 19.50 or 19.00 when viewing it in my PHP I see either 19.5 or 19. Is there a way that I see the two decimal places whether there is a zero or not.

How can I minipulate this whether in the MySQL or PHP.

Thank you,
AdonaiEchad
This topic has been closed for replies.

2 replies

Inspiring
April 4, 2007
AdonaiEchad wrote:
> I have made a database where I want to list certain items and their prices. In
> MySQL I had set my prices to be the type to be double. In my PHP when I type
> lets say 19.95 when I see it live on my PHP page it shows $19.95 which is great
> and what I want. However the problem lies when I want to list something that
> has 19.50 or 19.00 when viewing it in my PHP I see either 19.5 or 19. Is there
> a way that I see the two decimal places whether there is a zero or not.

Storing currencies in a database is fraught with problems because
floating point numbers can sometimes produce odd rounding errors when
used in calculations. The most practical way is to store currencies as
integers, rather than floating point numbers. In other words, store all
currencies as cents, not dollars and cents. Then divide by 100. Use the
PHP number_format() function to format the output to two decimal places.

--
David Powers, Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Inspiring
April 4, 2007
On 04 Apr 2007 in macromedia.dreamweaver.appdev, AdonaiEchad wrote:

> I have made a database where I want to list certain items and their
> prices. In MySQL I had set my prices to be the type to be double.
> In my PHP when I type lets say 19.95 when I see it live on my PHP
> page it shows $19.95 which is great and what I want. However the
> problem lies when I want to list something that has 19.50 or 19.00
> when viewing it in my PHP I see either 19.5 or 19. Is there a way
> that I see the two decimal places whether there is a zero or not.
>
> How can I minipulate this whether in the MySQL or PHP.

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_format

--
Joe Makowiec
http://makowiec.net/
Email: http://makowiec.net/contact.php
April 4, 2007
I kinda understand what it is saying...

So if I have a sql statement that has.

SELECT id, name, price
FROM products

So the statement should be like this...

SELECT id, name, FORMAT(price, ###,###.##)
FROM products

Is this correct?