php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42276 number_format returns wrong values for big numbers
Submitted: 2007-08-12 03:52 UTC Modified: 2007-08-12 10:47 UTC
From: eb1024 at gmail dot com Assigned:
Status: Not a bug Package: Strings related
PHP Version: 4.4.7 OS: Windows XP / Fedora / (Others?)
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: eb1024 at gmail dot com
New email:
PHP Version: OS:

 

 [2007-08-12 03:52 UTC] eb1024 at gmail dot com
Description:
------------
Hello, I've used the number_format function to add a thousands separator to a very large number (the 1000th Fibonacci number), the thing is that after using the number_format function the number which originally was

43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875

becomes,

43466557686937454881639861284285048836044564808501085299792936936934507578446811952241727283693776165701110466238201810459653426841499747856972973922645842460198740218733277412586280377875508003628944833118208

(or better yet):

43 466 557 686 937 454 881 639 861 284 285 048 836 044 564 808 501 085 299 792 936 936 934 507 578 446 811 952 241 727 283 693 776 165 701 110 466 238 201 810 459 653 426 841 499 747 856 972 973 922 645 842 460 198 740 218 733 277 412 586 280 377 875 508 003 628 944 833 118 208

As you can see the difference is huge! I tried it under PHP 5.2.1 and PHP 4.4 under Windows XP and Fedora Core 6.

More info @ http://www.alixaxel.com/wordpress/2007/05/19/php-math-library/ if you need it!

Reproduce code:
---------------
$fibonacci_1000 = '43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875';

echo number_format($fibonacci_1000, 0, '', ' ');

Expected result:
----------------
43 466 557 686 937 456 435 688 527 675 040 625 802 564 660 517 371 780 402 481 729 089 536 555 417 949 051 890 403 879 840 079 255 169 295 922 593 080 322 634 775 209 689 623 239 873 322 471 161 642 996 440 906 533 187 938 298 969 649 928 516 003 704 476 137 795 166 849 228 875

Actual result:
--------------
43 466 557 686 937 454 881 639 861 284 285 048 836 044 564 808 501 085 299 792 936 936 934 507 578 446 811 952 241 727 283 693 776 165 701 110 466 238 201 810 459 653 426 841 499 747 856 972 973 922 645 842 460 198 740 218 733 277 412 586 280 377 875 508 003 628 944 833 118 208

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-08-12 08:10 UTC] missingno at ifrance dot com
As per the doc, number_format() is meant to format floating point values (see http://php.net/number_format), not "number" values (such as strings composed of only digits).

The following code should do what you intended:

<?php

$fibonacci_1000 =
'43466557686937456435688527675040625802564660517371780402481729089536555
417949051890403879840079255169295922593080322634775209689623239873322471
161642996440906533187938298969649928516003704476137795166849228875';

function string_number_format($s)
{
$s = strrev($s); // Needed to avoid '1234' being chunked as '123' & '4'.
/*
str_split() is a PHP 5 only function...
if you also need to support PHP 4, you can use chunk_split()/wordwrap() + explode() to get the same thing.
*/
$s = str_split($s, 3); // $s is now an array dividing the string at thousands (but using the reversed string)
$s = implode(' ', $s); // Glue thousands together using spaces as separator.
$s = strrev($); // Put everything back in order.
return $s;
}

echo string_number_format($fibonacci_1000);

?>

Please note that this function is really basic, you may need to adjust it to your needs.

Hope this helps.
 [2007-08-12 10:47 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

That's because number_format takes a float, and not a string. And floats have a maximum precision.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 17:01:30 2024 UTC