php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #26952 printf()'s implementation of length/precision is inconsistent w/ other language
Submitted: 2004-01-17 22:34 UTC Modified: 2004-08-30 11:39 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: pluggz24 at yahoo dot com Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: Irrelevant OS: windows
Private report: No CVE-ID: None
 [2004-01-17 22:34 UTC] pluggz24 at yahoo dot com
Description:
------------
from the first user comment on http://us2.php.net/printf,
(not written by me):

I'm not sure if it is a feature or a bug :) but it's definately a gotcha! The length.precison in a printf float is different from any other language's printf.  The difference is outlined below in a small program written in C, PERL, and PHP using the printf function:

In C:
...>cat pf-test.c
main() {
   printf("12345678901234567890\n");
   printf("%09.2f\n",22.0/7.0);
}
...>gcc pf-test.c -o pf-test
...>./pf-test
12345678901234567890
000003.14

NOTE: Nine *total* displayed chars, two behind the decimal

Now PERL:
...>cat pf-test.pl
#!/usr/bin/perl
printf("12345678901234567890\n");
printf("%09.2f\n",22.0/7.0);
...>perl pf-test.pl
12345678901234567890
000003.14

NOTE: *Identical* to 'C' (and awk, and ...)

Now PHP:
...>cat pf-test.php
<?php
   printf("123456789012340567890\n");
   printf("%09.2f\n",22.0/7.0);
?>
...>php -q pf-test.php
123456789012340567890
000000003.14

NOTE: Nine digits before the decimal, the decimal, and two places after.

This tends to hide in proportional fonts, but can cause wierd alignment issues and gaps, particularly if you are using pre-formatted mono-spaced text.

Reproduce code:
---------------
<?php
   printf("123456789012340567890\n");
   printf("%09.2f\n",22.0/7.0);
?>

Expected result:
----------------
12345678901234567890
000003.14

Actual result:
--------------
123456789012340567890
000000003.14

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-08-30 11:39 UTC] dave@php.net
Thank you for your bug report. This issue has already been fixed
in the latest released version of PHP, which you can download at 
http://www.php.net/downloads.php

This has been fixed as of 4.3.7 and 5.0.0.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 27 17:01:29 2024 UTC