Solved

Php and money calculations best practice

Posted on 2015-01-15
5
237 Views
Last Modified: 2015-01-15
Hi all. In a table I have set the 'price' field as decimal(6,2). Now I'm going to write the code to manage prices and I read that the best practice is to treat all currency values as integer, dividing the currency value per 100, then do calculation and finally formatting the result in a readable format.

Is this correct?
And can I get from the db a decimal value covert it in integer do calcs and then format the result or it is better to change the field type and store values as integers?

I wish to know the best professional practice.
Thank you all in advance
0
Comment
Question by:Marco Gasi
  • 3
  • 2
5 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
decimal(6,2) is 9999.99 maximum.  I know the 'old' method used to be to use 'int's for money.  But I don't do that.  The data sent to and retrieved from the database is text.  If I need a number formatted as currency, I use 'sprintf' to do it.  Like this:
sprintf("%.2f", $price)

Open in new window

http://php.net/manual/en/function.sprintf.php
0
 
LVL 30

Author Comment

by:Marco Gasi
Comment Utility
Hi, Dave, thanks for your reply. Let me understand: are you saying I have to set all fields as text/varchar?
And formatting problems apart (I know sprintf), calculate a sum of several values or the tax percentage must be done with text values? I'm confusing...
0
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
Comment Utility
No, I'm not.  I'm saying that the data in queries from PHP to MySQL and back is always in text format, even if the column is a number type in the table.  Yes, there are a couple of binary formats but nothing that applies here.

In order to force PHP to view variable data as a number, I often multiply it by 1 or add 0 to it.  When I need to like in adding tax to an order, I do that, add the numbers, and use 'sprintf' to reformat the result for the display and/or database.
0
 
LVL 30

Author Closing Comment

by:Marco Gasi
Comment Utility
Thank you Dave
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
You're welcome!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now