php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78265 printf does not properly format decimals when using '%.8F'
Submitted: 2019-07-09 06:43 UTC Modified: 2019-07-09 07:50 UTC
From: mmucklo at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: I18N and L10N related
PHP Version: 7.3.7 OS: Linux / any
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mmucklo at gmail dot com
New email:
PHP Version: OS:

 

 [2019-07-09 06:43 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-07-09 07:28 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2019-07-09 07:28 UTC] cmb@php.net
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>.
 [2019-07-09 07:50 UTC] mmucklo at gmail dot com
Sigh - RTM and you’re absolutely correct, expected behavior.

Back to figuring out the locale related issue the user was having which is clearly due to some other reason.

Thanks for your time and sorry for not catching that beforehand.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 14:01:32 2024 UTC