php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64038 number_format isn't correct with big numbers
Submitted: 2013-01-21 16:04 UTC Modified: 2013-01-21 18:37 UTC
From: nicolas dot giraud at actiane dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: Irrelevant OS:
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: nicolas dot giraud at actiane dot com
New email:
PHP Version: OS:

 

 [2013-01-21 16:04 UTC] nicolas dot giraud at actiane dot com
Description:
------------
The number_format function don't give the good result with good format, maybe 
because of cast in float that kills precision.


Test script:
---------------
<?php
$number = '123456789012345678901234567890';
$formatted = number_format($number, 2, '.', ' ');
echo $formatted;

?>

Expected result:
----------------
123 456 789 012 345 678 901 234 567 890.00

Actual result:
--------------
123 456 789 012 345 660 285 533 552 640.00

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-01-21 16:18 UTC] rasmus@php.net
-Status: Open +Status: Not a bug
 [2013-01-21 16:18 UTC] rasmus@php.net
Not much we can do about that. number_format() takes a float and you are 
overflowing a float with your huge value. Since you already have it as a string, 
you could just throw a regex at it. Something like:
 preg_replace("/(?<=\d)(?=(\d{3})+(?!\d))/",",",$number);
 [2013-01-21 18:24 UTC] nicolas dot giraud at actiane dot com
Why number_format can't accept a mixed value?
Cast into a string and parse it correctly, it would be quicker as using regexp...
And when expected result is not the same as actual result, it's a bug, isn't it?
 [2013-01-21 18:37 UTC] rasmus@php.net
No, it isn't a bug because the function is documented to take a float. So it is 
behaving exactly as documented. And no, it can't take a mixed type because in 
order to do the rounding that it supports, it needs to work with a numeric type.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue May 13 09:01:27 2025 UTC