Jump to content

Is converting product price to cents the best way to store value in the database?


imgrooot

Recommended Posts

Say I have two functions that convert from Dollar to Cents and Cents to Dollar.

function convertToDollar($value) {
    $amount = $value / 100;
    return $amount;
}

function convertToCents($value) {
    $amount = $value * 100;
    return $amount;
}

Currently I convert all the Dollar amounts to cents and store the cents in the mysql database column. When I showcase those amounts on a page, I simply convert the Cents back to Dollar.

Am I doing this correctly or is there a better way to store product pricing in mysql table?

Link to comment
Share on other sites

I store prices as decimal EG

+-------------+------------------+------+-----+----------------------+--------------------------------+
| Field       | Type             | Null | Key | Default              | Extra                          |
+-------------+------------------+------+-----+----------------------+--------------------------------+
| prod_id     | int(11)          | NO   | PRI | NULL                 | auto_increment                 |
| description | varchar(50)      | YES  |     | NULL                 |                                |
| price       | decimal(10,2)    | YES  |     | NULL                 |                                |
+-------------+------------------+------+-----+----------------------+--------------------------------+

 

Query example...

TABLE: product                                     TABLE: cart
+---------+-------------+--------+                 +----+---------+------+
| prod_id | description | price  |                 | id | prod_id | qty  |
+---------+-------------+--------+                 +----+---------+------+
|       1 | Product AZ  |  49.99 |                 |  1 |       1 |    2 |
|       2 | Product B   |  29.99 |                 |  2 |       3 |    5 |
|       3 | Product C   |   9.99 |                 |  3 |       7 |    1 |
|       4 | Product D   |  22.99 |                 |  4 |       6 |    2 |
|       5 | Product E   |  29.99 |                 +----+---------+------+
|       6 | Product F   |  19.99 |
|       7 | Product G   | 129.99 |
|       8 | Product H   |  99.99 |
|       9 | Product I   |  74.99 |
|      10 | Product J   |  69.99 |
+---------+-------------+--------+

SELECT p.prod_id
     , p.description
     , p.price
     , c.qty
     , p.price * c.qty as total
FROM test_product p
         JOIN
     test_cart c USING (prod_id);
     
+---------+-------------+--------+------+--------+
| prod_id | description | price  | qty  | total  |
+---------+-------------+--------+------+--------+
|       1 | Product AZ  |  49.99 |    2 |  99.98 |
|       3 | Product C   |   9.99 |    5 |  49.95 |
|       7 | Product G   | 129.99 |    1 | 129.99 |
|       6 | Product F   |  19.99 |    2 |  39.98 |
+---------+-------------+--------+------+--------+

 

Link to comment
Share on other sites

To explain, the thing about converting to cents is because integers are precise while floating-point numbers (non-integers) are not precise. And when it comes to currency, being precise is very important.
What DECIMAL does is give you something that looks like a non-integer, except it's precise. In exchange for the precision, you have to tell the database exactly how precise you want and you won't be able to store anything smaller. But that's perfectly fine for you because you don't care about anything more than two decimal places for cents.

Link to comment
Share on other sites

49 minutes ago, Barand said:

I store prices as decimal EG


+-------------+------------------+------+-----+----------------------+--------------------------------+
| Field       | Type             | Null | Key | Default              | Extra                          |
+-------------+------------------+------+-----+----------------------+--------------------------------+
| prod_id     | int(11)          | NO   | PRI | NULL                 | auto_increment                 |
| description | varchar(50)      | YES  |     | NULL                 |                                |
| price       | decimal(10,2)    | YES  |     | NULL                 |                                |
+-------------+------------------+------+-----+----------------------+--------------------------------+

 

Query example...


TABLE: product                                     TABLE: cart
+---------+-------------+--------+                 +----+---------+------+
| prod_id | description | price  |                 | id | prod_id | qty  |
+---------+-------------+--------+                 +----+---------+------+
|       1 | Product AZ  |  49.99 |                 |  1 |       1 |    2 |
|       2 | Product B   |  29.99 |                 |  2 |       3 |    5 |
|       3 | Product C   |   9.99 |                 |  3 |       7 |    1 |
|       4 | Product D   |  22.99 |                 |  4 |       6 |    2 |
|       5 | Product E   |  29.99 |                 +----+---------+------+
|       6 | Product F   |  19.99 |
|       7 | Product G   | 129.99 |
|       8 | Product H   |  99.99 |
|       9 | Product I   |  74.99 |
|      10 | Product J   |  69.99 |
+---------+-------------+--------+

SELECT p.prod_id
     , p.description
     , p.price
     , c.qty
     , p.price * c.qty as total
FROM test_product p
         JOIN
     test_cart c USING (prod_id);
     
+---------+-------------+--------+------+--------+
| prod_id | description | price  | qty  | total  |
+---------+-------------+--------+------+--------+
|       1 | Product AZ  |  49.99 |    2 |  99.98 |
|       3 | Product C   |   9.99 |    5 |  49.95 |
|       7 | Product G   | 129.99 |    1 | 129.99 |
|       6 | Product F   |  19.99 |    2 |  39.98 |
+---------+-------------+--------+------+--------+

 

Wow that's a wonderful example.

I just have one question. Why use (10,2) instead of say (12,2)? Wouldn't it be safer to use a higher threshold if you're expecting bigger to store bigger numbers? 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.