Bug #78265 [Opn->Nab]: printf does not properly format decimals when using '%.8F'
| From: | cmb@php.net | Date: | Tue, 09 Jul 2019 07:28:37 +0000 |
| Subject: | Bug #78265 [Opn->Nab]: printf does not properly format decimals when using '%.8F' | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-221666@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=78265&edit=1
ID: 78265
Updated by: cmb@php.net
Reported by: mmucklo at gmail dot com
Summary: printf does not properly format decimals when using
'%.8F'
-Status: Open
+Status: Not a bug
Type: Bug
Package: I18N and L10N related
Operating System: Linux / any
PHP Version: 7.3.7
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
See <https://www.php.net/manual/en/function.printf.php#refsect1-function.printf-parameters>.
Previous Comments:
------------------------------------------------------------------------
[2019-07-09 06:43:17] mmucklo at gmail dot com
Description:
------------
printf does not properly format uppercase floats.
This is in the master branch and goes as far back as 5.6.40, but I didn't trace back any
further.
The problem code is here:
https://github.com/php/php-src/blob/master/main/spprintf.c#L639
It only pays attention to a lower case 'f'
WARNING - FIXING this could cause unsuspected results as this bug has been present as far back as
5.6.40 at least (the furthest back I looked).
Specifically the code in microtime calls into the printf function (eventually), and this will change
the output of microtime() to be locale-specific.
https://github.com/php/php-src/blob/master/ext/standard/microtime.c#L81
This is not necessarily a bad thing, but there are perhaps libraries that expect the buggy behavior
at this point (e.g. one that I presently maintain: https://github.com/mmucklo/DtcQueueBundle/blob/5.0.0/Util/Util.php#L104-L105)
I'm not suggesting that it should not get fixed, but just to be cautious maybe where in the
release cycle such a patch should go.
As a reference (couldn't instantly find a C version, but this CPP version seems accurate):
http://www.cplusplus.com/reference/cstdio/printf/
Also this Stackoverflow seems to clarify things. Pay particular attention to the second answer
(about nan and inf capitalization), as I think PHP may do that wrong as well, although I didn't
test it, but just looking at the code in spprintf.c, I don't see any specific logic to handle
that case.
https://stackoverflow.com/questions/34706228/difference-between-upper-and-lower-case-double-float-type-specifiers-in-c
Test script:
---------------
<?php
// NOTE your system needs to have this locale loaded first
// On ubuntu 19 which I'm using (via docker) do the following
// from the shell prompt (if you are not root, become root, or prefix with sudo):
//
// locale-gen de_DE
// locale-gen de_DE.UTF-8
// dpkg-reconfigure locales
//
// # you may also need the locales package installed (apt install locales).
//
$loc_de = setlocale(LC_ALL, 'de_DE.UTF-8', 'de_DE', 'de',
'ge');
printf("%0.8f\n", 0.12345);
printf("%0.8F\n", 0.12335);
// One should show the comma, the other won't
/**
Here is some C code to reproduce the expected result
#include <stdio.h>
#include <locale.h>
int main(int argc, char**argv) {
setlocale(LC_ALL, "de_DE.UTF-8");
printf("%.8f\n", 0.12345);
printf("%.8F\n", 0.12335);
return 0;
}
*/
Expected result:
----------------
0,12345000
0,12335000
Actual result:
--------------
0,12345000
0.12335000
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=78265&edit=1