|  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
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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
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
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.


Add a Patch

Pull Requests

Add a Pull Request


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
 [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]
-Type: Bug +Type: Feature/Change Request
 [2020-03-05 10:05 UTC]
ICU has unum_formatDecimal()[1]:

| The syntax of the input number is a "numeric string" as defined
| in the Decimal Arithmetic Specification, available at

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

[1] <>
 [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 to create a Formattable from the string input, and then call formatUFormattable() on that:

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].

 [2021-01-28 20:39 UTC] cananian at wikimedia dot org
This is the root cause of Wikimedia bug T268456, fwiw:

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:
 [2021-09-16 13:02 UTC]
> 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] <>
 [2021-11-11 11:18 UTC]
-Package: intl +Package: I18N and L10N related
 [2021-12-15 19:47 UTC] cananian at wikimedia dot org
github pr is now at

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

I have a followup for formatCurrency at -- as currently written it required a new method in ICU, but as documented in 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-2022 The PHP Group
All rights reserved.
Last updated: Sun Nov 27 16:05:53 2022 UTC