Solved

PHP Float numbers

Posted on 2011-02-22
12
478 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

786 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