php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78292 NumberFormatter procedural functions don't work
Submitted: 2019-07-15 09:52 UTC Modified: 2019-07-15 10:55 UTC
From: delboy1978uk at gmail dot com Assigned:
Status: Not a bug Package: intl (PECL)
PHP Version: 7.1.30 OS: any
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: delboy1978uk at gmail dot com
New email:
PHP Version: OS:

 

 [2019-07-15 09:52 UTC] delboy1978uk at gmail dot com
Description:
------------
When using numfmt_create() and numfmt_currency(), PHP flakes out and gives us some number in Scientific Notation, instead of formatting like US currency.

Look here https://3v4l.org/QgS7U 



Test script:
---------------
<?php

$fmt = numfmt_create('en_US', NumberFormatter::TYPE_CURRENCY );
echo numfmt_format_currency($fmt, 32.00, "USD")."\n";

$fmt = new NumberFormatter( 'en_US', NumberFormatter::CURRENCY );
echo $fmt->formatCurrency(32.00, "USD")."\n";

/* output as follows

3.2E1 
$32.00

Expected result:
----------------
$32.00 
$32.00

Actual result:
--------------
3.2E1 
$32.00

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-07-15 10:00 UTC] matthew at matthewsetter dot com
In addition to the bug description provided, I have the following example, using the procedural functions:

$fmt = numfmt_create('en_US', NumberFormatter::DECIMAL);
echo numfmt_format_currency($fmt, 32.00, "EUR")."\n";

I would expect that this would return 32,00, however, it returns 32. I've used a number of ISO 4217 currency codes (specifically USD, EUR, AUD, GBP) but the output remains the same. It's worth noting that if the float value provided to numfmt_format_currency contains a fractional part, in addition to the integral part, then the number formats as expected. 

Environment Details: PHP 7.3.6 / macOS 10.14.5.
 [2019-07-15 10:10 UTC] cmb@php.net
-Summary: NumberFormatter procedural functions dont work +Summary: NumberFormatter procedural functions don't work -Status: Open +Status: Verified -Package: Unknown/Other Function +Package: intl
 [2019-07-15 10:16 UTC] delboy1978uk at gmail dot com
-Status: Verified +Status: Closed
 [2019-07-15 10:16 UTC] delboy1978uk at gmail dot com
closing, as it turns out the dev was using TYPE_CURRENCY and not just CURRENCY. It works after the change
 [2019-07-15 10:21 UTC] xatenev@php.net
From the docs: https://www.php.net/manual/en/class.numberformatter.php

`These styles are used by the numfmt_create() to define the type of the formatter.` 

... NumberFormatter:CURRENCY ...

`These constants define how the numbers are parsed or formatted. They should be used as arguments to numfmt_format() and numfmt_parse().`: 

... NumberFormatter::TYPE_CURRENCY ...

Wrong parameter choosen here. :)


-------------

To the problem in the comments with decimal

Original code:

<?php

$fmt = numfmt_create('en_US', NumberFormatter::DECIMAL);
echo numfmt_format_currency($fmt, 32.00, "EUR")."\n";

Fixed code:

<?php

$fmt = numfmt_create('en_US', NumberFormatter::DECIMAL);
numfmt_set_attribute($fmt, NumberFormatter::MIN_FRACTION_DIGITS, 2);
echo numfmt_format_currency($fmt, 32.00, "EUR")."\n";

https://3v4l.org/IRhPQ
 [2019-07-15 10:55 UTC] cmb@php.net
-Status: Closed +Status: Not a bug
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Feb 18 21:01:28 2020 UTC