Solved

Php and money calculations best practice

Posted on 2015-01-15
5
290 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
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.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

734 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