Solved

Php and money calculations best practice

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
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.

628 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