imgrooot Posted November 10, 2019 Share Posted November 10, 2019 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? Quote Link to comment https://forums.phpfreaks.com/topic/309504-is-converting-product-price-to-cents-the-best-way-to-store-value-in-the-database/ Share on other sites More sharing options...
Barand Posted November 10, 2019 Share Posted November 10, 2019 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 | +---------+-------------+--------+------+--------+ Quote Link to comment https://forums.phpfreaks.com/topic/309504-is-converting-product-price-to-cents-the-best-way-to-store-value-in-the-database/#findComment-1571431 Share on other sites More sharing options...
requinix Posted November 10, 2019 Share Posted November 10, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/309504-is-converting-product-price-to-cents-the-best-way-to-store-value-in-the-database/#findComment-1571433 Share on other sites More sharing options...
imgrooot Posted November 10, 2019 Author Share Posted November 10, 2019 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? Quote Link to comment https://forums.phpfreaks.com/topic/309504-is-converting-product-price-to-cents-the-best-way-to-store-value-in-the-database/#findComment-1571437 Share on other sites More sharing options...
Barand Posted November 10, 2019 Share Posted November 10, 2019 If you are selling warships, why not indeed. Whatever your application requires. 1 Quote Link to comment https://forums.phpfreaks.com/topic/309504-is-converting-product-price-to-cents-the-best-way-to-store-value-in-the-database/#findComment-1571439 Share on other sites More sharing options...
imgrooot Posted November 10, 2019 Author Share Posted November 10, 2019 5 hours ago, Barand said: If you are selling warships, why not indeed. Whatever your application requires. Ah good to know lol. Quote Link to comment https://forums.phpfreaks.com/topic/309504-is-converting-product-price-to-cents-the-best-way-to-store-value-in-the-database/#findComment-1571446 Share on other sites More sharing options...
Barand Posted November 10, 2019 Share Posted November 10, 2019 Plus I doubt if you'd be interested in the odd cents if you are selling a $5,000,000,000 aircraft carrier. Although if your prices were in guineas... Quote Link to comment https://forums.phpfreaks.com/topic/309504-is-converting-product-price-to-cents-the-best-way-to-store-value-in-the-database/#findComment-1571448 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.