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-09-16 13:02 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: intl (PECL)
PHP Version: 7.2.3 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
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

Add a Patch

Pull Requests

Add a Pull Request

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>
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Sep 19 16:03:36 2021 UTC