|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52894 number_format fails with large values
Submitted: 2010-09-20 17:53 UTC Modified: 2010-09-21 18:57 UTC
From: info at tellmatic dot org Assigned:
Status: Not a bug Package: *Math Functions
PHP Version: 5.2.14 OS: linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
41 - 16 = ?
Subscribe to this entry?

 [2010-09-20 17:53 UTC] info at tellmatic dot org
wrong output, not as give, drifts a lot!

Test script:
number_format("1234.560000", 50, '.', '');

Expected result:
expected output should be 1234.56 with additional 48 zeros! and not a 'random value'.

Actual result:
== 1234.55999999999994543031789362430572509765625000000000

can't be!


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-09-20 18:24 UTC]
-Status: Open +Status: Bogus
 [2010-09-20 18:24 UTC]
Floating point values have a limited precision. Hence a value might 
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly 
printing it without any mathematical operations.

If you would like to know more about "floats" and what IEEE
754 is, read this:
Thank you for your interest in PHP.

 [2010-09-21 14:48 UTC] info at tellmatic dot org
number_format should 'format' numbers, not recalculate, or? thats how i understand the function.
 [2010-09-21 18:57 UTC]
Yes, that is all number_format does.  But, you asked it for 50 digits of 
precision for 1234.560000 which just isn't possible for computers to represent 
given the way they they handle floating point numbers.  It has nothing to do 
with number_format.  You will see the same if you do:

echo 1234.560000;

If you need super-precise floating point, you can't use the computers built-in 
floating point support.  You will need to use instead.  The 
big drawback is that it is much much slower than the computer's native floating 
point support.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jul 20 05:01:30 2024 UTC