|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46145 printf() %e format should make 2 digit exponent
Submitted: 2008-09-21 19:44 UTC Modified: 2021-05-03 12:51 UTC
From: lbayuk at users dot sourceforge dot net Assigned: cmb (profile)
Status: Wont fix Package: Strings related
PHP Version: 5.2.6 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: lbayuk at users dot sourceforge dot net
New email:
PHP Version: OS:


 [2008-09-21 19:44 UTC] lbayuk at users dot sourceforge dot net
PHP printf, sprintf, and fprintf with %e scientific notation produce a 1 digit exponent for values less than 1e10.  Other implementation of functions by the same name produce a minimum of 2 digits in the exponent, as the C standard requires. Although it can be argued that PHP is not bound by other standards, the following comment in main/snprintf.c function php_conv_fp() [line 545] leads one to believe this is a PHP bug:
             * Make sure the exponent has at least 2 digits

Reproduce code:
printf("%8.2e\n", 1000000);

Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2008-09-28 23:07 UTC] ilia dot cheishvili at gmail dot com
The patch to fix this is here, along with all of the unit tests that had to be modified to take the new, correct behavior into account:
 [2008-09-29 01:07 UTC] lbayuk at pobox dot com
After taking a look at your patch, approximately:
          * Make sure the exponent has at least 2 digits
+        if (t_len <= 1) {
+               *s++ = '0';
+        }
         while (t_len--) {

I got curious, since the code you added is described by the existing comment. So I checked back through CVS and sure enough similar code was there a while ago and was removed. It was:
         if (t_len == 1)
                *s++ = '0';
So it looks like the 2-digit minimum exponent code was *removed*. Certainly we would not want to put the code back in until we determine why it came out... perhaps there was a good reason. I haven't yet located the exact file version where the change was made.
 [2015-06-22 21:52 UTC] lbayuk at users dot sourceforge dot net
-: lbayuk at pobox dot com +: lbayuk at users dot sourceforge dot net -Package: Feature/Change Request +Package: *General Issues
 [2015-06-22 21:52 UTC] lbayuk at users dot sourceforge dot net
(Verified still there in PHP-5.6.10 and fix email)
 [2019-10-18 11:58 UTC]
-Type: Feature/Change Request +Type: Bug -Package: *General Issues +Package: Strings related
 [2019-10-18 12:04 UTC]
Seems this behaviour is the same since at least PHP 4.3:
 [2021-05-03 12:51 UTC]
-Status: Open +Status: Wont fix -Assigned To: +Assigned To: cmb
 [2021-05-03 12:51 UTC]
That change has been introduced with commit d3e97520d9b[1], and
might not have been done deliberately.  Given the long standing
behavior, and that it is even documented[2], I don't think that we
should change that again.  If anybody feels that there should be
two digits at minimum, please pursue the RFC process[3].

[1] <>
[2] <>
[3] <>

Is this still an issue with any of the actively supported PHP

[1] <>
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Fri May 14 02:01:23 2021 UTC