php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #76093 numfmt_format should accept string numbers rather than casting to float
Submitted: 2018-03-14 03:08 UTC Modified: 2021-11-11 11:18 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: Wes dot nospam at example dot org Assigned:
Status: Open Package: I18N and L10N related
PHP Version: 7.2.3 OS:
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: Wes dot nospam at example dot org
New email:
PHP Version: OS:

 

 [2018-03-14 03:08 UTC] Wes dot nospam at example dot org
Description:
------------
If I understand correctly, ICU supports arbitrary precision decimal numbers formatting, but the php function converts strings to float, losing precision, before giving them to ICU.

https://github.com/php/php-src/blob/master/ext/intl/formatter/formatter_format.c#L72



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-03-14 03:21 UTC] Wes dot example at example dot org
the "numeric string" format function in ICU is here
http://icu-project.org/apiref/icu4c/classicu_1_1NumberFormat.html#aee2b88a01333eb699cbdca327e62841c
 [2018-03-14 07:03 UTC] Wes dot example at example dot org
and of course the same goes for 
public string formatCurrency ( float $value , string $currency )
which should too accept "numeric strings"...
 [2018-03-18 21:39 UTC] stas@php.net
-Type: Bug +Type: Feature/Change Request
 [2020-03-05 10:05 UTC] cmb@php.net
ICU has unum_formatDecimal()[1]:

| The syntax of the input number is a "numeric string" as defined
| in the Decimal Arithmetic Specification, available at
| http://speleotrove.com/decimal

From a quick glimpse, this looks quite different to PHP's numeric
strings, so supporting it wouldn't be straight forward.

[1] <https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/unum_8h.html#a59870a322f012dc1b9d99cf8a7b708f1>
 [2021-01-22 21:02 UTC] cananian at wikimedia dot org
libicu has the code to create the Decimal Number for you.

You'd use https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/classicu_1_1Formattable.html#a0505d26515fa1bbac3dd46a7e15e120e to create a Formattable from the string input, and then call formatUFormattable() on that: https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/unum_8h.html#aec903c32ade7d632fb7125a5dfd667ea

Should be pretty straight-forward.
 [2021-01-28 14:35 UTC] mail at lucaswerkmeister dot de
Hm, I don’t see so many differences between General Decimal Arithmetic numeric strings [1] and PHP numeric strings [2]:

1. PHP allows leading and trailing whitespace, GDA does not.
2. GDA includes special values (s)NaN and Inf(inity), PHP does not.

The syntax for finite numbers themselves is identical, as far as I can tell. (For instance, both allow lower- and uppercase E in exponential notation, empty digits before xor after decimal separator, only ASCII digits 0-9, leading zeroes anywhere…) Maybe this was different before PHP 8’s improved numeric strings [3].

[1]: http://speleotrove.com/decimal/daconvs.html#refnumsyn
[2]: https://www.php.net/manual/en/language.types.numeric-strings.php
[3]: https://wiki.php.net/rfc/saner-numeric-strings
 [2021-01-28 20:39 UTC] cananian at wikimedia dot org
This is the root cause of Wikimedia bug T268456, fwiw: https://phabricator.wikimedia.org/T268456

I have a patch written, I'll post it shortly.

There's a similar issue with currency formatting, but that requires an upstream patch to libicu: https://unicode-org.atlassian.net/browse/ICU-21479
 [2021-09-16 13:02 UTC] cmb@php.net
> I have a patch written, I'll post it shortly.

Any update?  Also, better submit a pull request[1] instead of
attaching a patch for better visibility.

[1] <https://github.com/php/php-src/pulls>
 [2021-11-11 11:18 UTC] nikic@php.net
-Package: intl +Package: I18N and L10N related
 [2021-12-15 19:47 UTC] cananian at wikimedia dot org
github pr is now at https://github.com/php/php-src/pull/7782

Sorry for the long delay!  It got lost in my tree for a while.

I have a followup for formatCurrency at https://github.com/cscott/php-src/tree/bug76093-followup -- as currently written it required a new method in ICU, but as documented in https://unicode-org.atlassian.net/browse/ICU-21479 this can actually be done with the current ICU API in a certain way; I need to update the follow up to do things That Way.
 [2021-12-15 21:13 UTC] cananian at wikimedia dot org
I've added the followup which handles formatCurrency() to the same PR.  So both patches are there now.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 06:01:30 2024 UTC