Bug #64038 [Nab]: number_format isn't correct with big numbers
| From: | rasmus@php.net | Date: | Mon, 21 Jan 2013 18:37:28 +0000 |
| Subject: | Bug #64038 [Nab]: number_format isn't correct with big numbers | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-176158@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=64038&edit=1
ID: 64038
Updated by: rasmus@php.net
Reported by: nicolas dot giraud at actiane dot com
Summary: number_format isn't correct with big numbers
Status: Not a bug
Type: Bug
Package: Unknown/Other Function
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2013-01-21 18:24:58] 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 16:18:00] 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 16:04:34] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=64038&edit=1