php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40802 number_format does not fill rounded number with zeros
Submitted: 2007-03-14 10:07 UTC Modified: 2007-03-14 10:34 UTC
From: taco at procurios dot nl Assigned:
Status: Not a bug Package: *Math Functions
PHP Version: 5.2.1 OS: Linux 2.6.17-11-386
Private report: No CVE-ID: None
 [2007-03-14 10:07 UTC] taco at procurios dot nl
Description:
------------
Since there is no way to tell number_format to use a specified decimal separator and thousands separator without rounding the number I wanted to set the amount of decimals bigger than the given amount of decimals to strip the trailing zeros afterwards.

However, the string number_format returned did not only add zeros to the given number:

$ php -v
PHP 5.2.1-0.dotdeb.1 with Suhosin-Patch 0.9.6.2 (cli) (built: Feb  9 2007 04:46:32) 
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
$ php -r "echo number_format(2.333333, 60, ',', '.');"
2,333333000000000101437080957111902534961700439453125000000000

Reproduce code:
---------------
echo number_format(2.333333, 60, ',', '.');

Expected result:
----------------
2,333333000000000000000000000000000000000000000000000000000000

Actual result:
--------------
2,333333000000000101437080957111902534961700439453125000000000

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-03-14 10:09 UTC] tony2001@php.net
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:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
 
Thank you for your interest in PHP.


 [2007-03-14 10:32 UTC] taco at procurios dot nl
I agree that this is normal behaviour for floats, but the same bug appears when you would pass a string. Does number_format cast strings to floats internally?
 [2007-03-14 10:34 UTC] taco at procurios dot nl
Nevermind. Forgot to look at the manual, the number argument should be a float...
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Wed Oct 28 03:01:23 2020 UTC