Skip to content

fix: number_to_currency() error on PHP 8.1 #5454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions system/Helpers/number_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,7 @@ function number_to_amount($num, int $precision = 0, ?string $locale = null)
}

if (! function_exists('number_to_currency')) {
/**
* @param string $locale
* @param int $fraction
*/
function number_to_currency(float $num, string $currency, ?string $locale = null, ?int $fraction = null): string
function number_to_currency(float $num, string $currency, ?string $locale = null, int $fraction = 0): string
{
return format_number($num, 1, $locale, [
'type' => NumberFormatter::CURRENCY,
Expand Down
1 change: 1 addition & 0 deletions tests/system/Helpers/NumberHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public function testQuadrillions()
*/
public function testCurrencyCurrentLocale()
{
$this->assertSame('$1,235', number_to_currency(1234.56, 'USD', 'en_US'));
$this->assertSame('$1,234.56', number_to_currency(1234.56, 'USD', 'en_US', 2));
$this->assertSame('£1,234.56', number_to_currency(1234.56, 'GBP', 'en_GB', 2));
$this->assertSame("1.234,56\u{a0}RSD", number_to_currency(1234.56, 'RSD', 'sr_RS', 2));
Expand Down
18 changes: 10 additions & 8 deletions user_guide_src/source/helpers/number_helper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Available Functions

The following functions are available:

.. php:function:: number_to_size($num[, $precision = 1[, $locale = null])
.. php:function:: number_to_size($num[, $precision = 1[, $locale = null]])

:param mixed $num: Number of bytes
:param int $precision: Floating point precision
Expand Down Expand Up @@ -86,21 +86,23 @@ The following functions are available:

echo number_to_amount('123,456,789,012', 2, 'de_DE'); // Returns 123,46 billion

.. php:function:: number_to_currency($num, $currency[, $locale = null])
.. php:function:: number_to_currency($num, $currency[, $locale = null[, $fraction = 0]])

:param mixed $num: Number to format
:param float $num: Number to format
:param string $currency: The currency type, i.e., USD, EUR, etc
:param string $locale: The locale to use for formatting
:param string|null $locale: The locale to use for formatting
:param integer $fraction: Number of fraction digits after decimal point
:returns: The number as the appropriate currency for the locale
:rtype: string

Converts a number in common currency formats, like USD, EUR, GBP, etc::

echo number_to_currency(1234.56, 'USD'); // Returns $1,234.56
echo number_to_currency(1234.56, 'EUR'); // Returns €1,234.56
echo number_to_currency(1234.56, 'GBP'); // Returns £1,234.56
echo number_to_currency(1234.56, 'YEN'); // Returns YEN1,234.56
echo number_to_currency(1234.56, 'USD', 'en_US', 2); // Returns $1,234.56
echo number_to_currency(1234.56, 'EUR', 'de_DE', 2); // Returns 1.234,56 €
echo number_to_currency(1234.56, 'GBP', 'en_GB', 2); // Returns £1,234.56
echo number_to_currency(1234.56, 'YEN', 'ja_JP', 2); // Returns YEN 1,234.56

If you don't specify a locale, the Request locale is used.

.. php:function:: number_to_roman($num)

Expand Down