PHP Float numbers

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,
 
LVL 2
pixalaxAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Beverley PortlockConnect With a Mentor Commented:
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
 
degarayCommented:
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
 
Dushan De SilvaTechnology ArchitectCommented:
Did you checked the values and the ranges you allocated under your database table design?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
pixalaxAuthor Commented:
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
 
Dushan De SilvaTechnology ArchitectCommented:
Try to dump variables before and after writing to database.
0
 
Ovid BurkeConsultant InstructorCommented:
It will calculate, and add a digit to complete the 5 decimal places. You should specify float(10,4) instead.
0
 
pixalaxAuthor Commented:
@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
 
Ovid BurkeConnect With a Mentor Consultant InstructorCommented:
I did overlook that didn't I? OK. Try changing your data type to double;
0
 
pixalaxAuthor Commented:
@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
 
Beverley PortlockConnect With a Mentor Commented:
MySQL DECIMAL fields will handle up to 65 digits of which 30 can be decimal.
0
 
pixalaxAuthor Commented:
Thank you for your help
0
 
Ovid BurkeConsultant InstructorCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.