Solved

PHP Float numbers

Posted on 2011-02-22
12
486 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: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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

791 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