Solved

PHP Float numbers

Posted on 2011-02-22
12
462 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
  • 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:Dushan911
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
 
LVL 17

Expert Comment

by:Dushan911
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 400 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 100 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 400 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
The viewer will learn how to count occurrences of each item in an array.

708 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

18 Experts available now in Live!

Get 1:1 Help Now