|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28975 printf("%6.2f") and values less than 1 gives wrong output
Submitted: 2004-06-30 17:40 UTC Modified: 2004-08-12 14:00 UTC
Avg. Score:4.6 ± 0.7
Reproduced:8 of 8 (100.0%)
Same Version:5 (62.5%)
Same OS:3 (37.5%)
From: phpbug at billix dot franken dot de Assigned:
Status: Closed Package: Output Control
PHP Version: Irrelevant 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: phpbug at billix dot franken dot de
New email:
PHP Version: OS:


 [2004-06-30 17:40 UTC] phpbug at billix dot franken dot de
A printf of float values less than 1 gives an incorrect output. It looks like the numbers before the dot equal the width specifier...

This happens with every tested PHP version (4.3.7, 5.0.0RC3 and CVS)

With PHP 4.3.6 both outputs were wrong...


Reproduce code:
$a = sprintf ( "%06.2f" , 0.5 );
$b = sprintf ( "%06.2f" , 1.5 );

Expected result:
string(6) "000.50"
string(6) "001.50"

Actual result:
string(9) "000000.50"
string(6) "001.50"


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2004-07-16 10:05 UTC] arattink at correct dot net
Line 401/403 in ext/standard/formatted_printf.c should be 
/*	if (precision > 0) { 
		width += (precision + 1);	 
I'm not a PHP-developer, but 'width' passed to 
php_sprintf_appenddouble should be passed on to 
php_sprintf_appendstring unaltered. It seems that in most cases 
precision is garbled to 0 so it works most of the time.
 [2004-07-19 10:18 UTC] christophe dot bidaux at netcourrier dot com
i have the same problem between the 4.3.4 and the 4.3.8 php version, on windows system...

i made some new tests and i think that when the value is less than 1, in 4.3.8, the number of digits before the decimal-point in the sprintf (6 in the example) is used as the number of digits before the decimal-point in the returned value (000000.50), whereas in the 4.3.4 version, this value is used for the length of the entire value (included the decimal-point).
 [2004-08-03 20:47 UTC] jwilliam at kcr dot uky dot edu
I also have the same problem after upgrading from 4.3.4 to 5.0.0.   Worked fine in 4.3.4.

 [2004-08-12 14:00 UTC] phpbug at billix dot franken dot de
By fixing bug #28633, this has been fixed as well ;.)
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Tue Nov 29 00:05:53 2022 UTC