From:             pluggz24 at yahoo dot com
Operating system: windows
PHP version:      Irrelevant
PHP Bug Type:     Feature/Change Request
Bug description:  printf()'s implementation of length/precision is inconsistent w/ 
other language

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

-- 
Edit bug report at http://bugs.php.net/?id=26952&edit=1
-- 
Try a CVS snapshot (php4):  http://bugs.php.net/fix.php?id=26952&r=trysnapshot4
Try a CVS snapshot (php5):  http://bugs.php.net/fix.php?id=26952&r=trysnapshot5
Fixed in CVS:               http://bugs.php.net/fix.php?id=26952&r=fixedcvs
Fixed in release:           http://bugs.php.net/fix.php?id=26952&r=alreadyfixed
Need backtrace:             http://bugs.php.net/fix.php?id=26952&r=needtrace
Need Reproduce Script:      http://bugs.php.net/fix.php?id=26952&r=needscript
Try newer version:          http://bugs.php.net/fix.php?id=26952&r=oldversion
Not developer issue:        http://bugs.php.net/fix.php?id=26952&r=support
Expected behavior:          http://bugs.php.net/fix.php?id=26952&r=notwrong
Not enough info:            http://bugs.php.net/fix.php?id=26952&r=notenoughinfo
Submitted twice:            http://bugs.php.net/fix.php?id=26952&r=submittedtwice
register_globals:           http://bugs.php.net/fix.php?id=26952&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=26952&r=php3
Daylight Savings:           http://bugs.php.net/fix.php?id=26952&r=dst
IIS Stability:              http://bugs.php.net/fix.php?id=26952&r=isapi
Install GNU Sed:            http://bugs.php.net/fix.php?id=26952&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=26952&r=float

Reply via email to