?
Solved

PHP Float numbers

Posted on 2011-02-22
12
Medium Priority
?
499 Views
Last Modified: 2012-06-27
Hello all,
I am having a problem with PHP float numbers. I am currently using "credit" cell is 10,5 float.
Let's say I have 3 values;

$currentCredit = 1000;
$newCredit= 950.4564;

 Everything seems normal, till I save it to database. It is adding extra number "950.45642" now where this last 2 came from? Where did I do wrong? I will be glad if anyone could help me.

Best regards,
 
0
Comment
Question by:pixalax
[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
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 4

Expert Comment

by:degaray
ID: 34957808
I am not quite sure why that happens, however you can solve it by delimitating your db field or by rounding up your variable

0
 
LVL 17

Expert Comment

by:Dushan De Silva
ID: 34957824
Did you checked the values and the ranges you allocated under your database table design?
0
 
LVL 2

Author Comment

by:pixalax
ID: 34957929
Thank you for your quick replies.
In my database, credit cells types are "float", 10 length and 5 decimals.

I have no idea why $newCredit= 950.4564; is recorded as $newCredit= 950.45642;
Shouldn't be $newCredit= 950.4560; by default since there are only 4 decimals?
0
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!

 
LVL 17

Expert Comment

by:Dushan De Silva
ID: 34957975
Try to dump variables before and after writing to database.
0
 
LVL 11

Expert Comment

by:Ovid Burke
ID: 34957988
It will calculate, and add a digit to complete the 5 decimal places. You should specify float(10,4) instead.
0
 
LVL 2

Author Comment

by:pixalax
ID: 34959206
@Dushan911;
I tried to dump the variables before adding to database. I'm using PDO, I did echo out my query and all seems fine. Since my sql query is fine, I believe either way PDO is changing something, either way  mysql database is changing something.

@madaboutasp;
I need float(10,5). What if later the result will be $newCredit= 950.45? Then I will have to change the database to float(10,2) ? I need float number with 5 decimals. I even tried number_format($newCredit,5); didn't help.
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 1600 total points
ID: 34959397
Some numbers do not render exactly into binary and this looks like a case of it. If you are working in normal currencies then the final result will be 2 decimals and the fifth palce decimal will not have much (if any) impact so I would not worry about it.

If those four decimals really matter then you may be better using a BIGINT and multiplying the number by 10,000 or looking at BCD coding (see http://uk.php.net/manual/en/intro.bc.php for more info). Many large financial systems store numbers as either integers or BCD simply to avoid the dec/binary conversion problem that you are experiencing. On IBM mid-range systems (I did 15 years on them) numbers are never stored in binary for exactly this reason.
0
 
LVL 11

Assisted Solution

by:Ovid Burke
Ovid Burke earned 400 total points
ID: 34959437
I did overlook that didn't I? OK. Try changing your data type to double;
0
 
LVL 2

Author Comment

by:pixalax
ID: 34959474
@bportlock;
I need 5 decimals. Many items buying price is like $0.00921, so calculating profit and everything... I need 5 decimals. I would also need to store maybe 10,000 or 20,000 (up to 99,999) so I changed it to decimal for now. I believe this should do it. I don't need to store more than 99,999 if taking a sum will not be a problem, I would like to leave it as decimal(10,5).

@madaboutasp;
I changed to decimal(10,5) just after my last comment (10:27 AM). Everything seems fine for now. All I need to know if storing up to 99,999.99999 as decimal(10,5) will create any problems? Such as taking sums, making some calculations?

Thank you for all of your time and concern.
0
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 1600 total points
ID: 34959507
MySQL DECIMAL fields will handle up to 65 digits of which 30 can be decimal.
0
 
LVL 2

Author Closing Comment

by:pixalax
ID: 34959536
Thank you for your help
0
 
LVL 11

Expert Comment

by:Ovid Burke
ID: 34959577
Glad you worked it out. Remember that the decimal format would not accept commas. As long as your numbers do not get rounded up to 100,000, you should not have a problem.
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

800 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