Solved

Php and money calculations best practice

Posted on 2015-01-15
5
270 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 83

Expert Comment

by:Dave Baldwin
ID: 40552155
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 31

Author Comment

by:Marco Gasi
ID: 40552169
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 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 40552197
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 31

Author Closing Comment

by:Marco Gasi
ID: 40552438
Thank you Dave
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40552465
You're welcome!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP Query return divisible by 3 3 25
Split wordpress loop 35 56
selector:validator cookies 4 29
PHP and MSSQL Arrays and Variables 3 22
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

792 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