php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #27916 Add support for %g (significant digits) format to printf functions
Submitted: 2004-04-07 22:36 UTC Modified: 2014-11-01 22:31 UTC
Votes:4
Avg. Score:4.0 ± 1.0
Reproduced:3 of 3 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (33.3%)
From: tcarter at noggin dot com dot au Assigned: requinix (profile)
Status: Closed Package: *General Issues
PHP Version: Irrelevant OS: Linux
Private report: No CVE-ID: None
 [2004-04-07 22:36 UTC] tcarter at noggin dot com dot au
Description:
------------
PHP's sprintf() (and other printf type functions) do not provide any method to specify the number of significant digits to be output, only field lengths and precision.

The glibc printf (3) man page lists a 'g' conversion specifier which behaves the same as 'd' but interprets the precision field as the number of significant digits to output rather than the number of digits to place after the decimal point.

Since PHP doesn't appear to have any existing built-in mechanism for formatting a value to a specified number of significant digits, I think that support for this should be added to sprintf().



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-11-01 22:31 UTC] requinix@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: requinix
 [2014-11-01 22:31 UTC] requinix@php.net
Added in 5.2.1 during "Changed double-to-string utilities to use BSD implementation"
https://github.com/php/php-src/commit/4d44a5b71dd74ea81d988f7f1e0ff8025bef5617#diff-e02d8864bd088660ded278bc073c2674R245

  printf('%1$.4e %1$.4f %1$.4g', 123.456);
  // 1.234e+2 123.4560 123.5
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Jun 18 05:01:31 2024 UTC