Skip to content

Commit db24b0b

Browse files
authored
Merge pull request #5454 from kenjis/fix-number_to_currency
fix: `number_to_currency()` error on PHP 8.1
2 parents d037bae + ce3d3cc commit db24b0b

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

system/Helpers/number_helper.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,7 @@ function number_to_amount($num, int $precision = 0, ?string $locale = null)
112112
}
113113

114114
if (! function_exists('number_to_currency')) {
115-
/**
116-
* @param string $locale
117-
* @param int $fraction
118-
*/
119-
function number_to_currency(float $num, string $currency, ?string $locale = null, ?int $fraction = null): string
115+
function number_to_currency(float $num, string $currency, ?string $locale = null, int $fraction = 0): string
120116
{
121117
return format_number($num, 1, $locale, [
122118
'type' => NumberFormatter::CURRENCY,

tests/system/Helpers/NumberHelperTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ public function testQuadrillions()
126126
*/
127127
public function testCurrencyCurrentLocale()
128128
{
129+
$this->assertSame('$1,235', number_to_currency(1234.56, 'USD', 'en_US'));
129130
$this->assertSame('$1,234.56', number_to_currency(1234.56, 'USD', 'en_US', 2));
130131
$this->assertSame('£1,234.56', number_to_currency(1234.56, 'GBP', 'en_GB', 2));
131132
$this->assertSame("1.234,56\u{a0}RSD", number_to_currency(1234.56, 'RSD', 'sr_RS', 2));

user_guide_src/source/helpers/number_helper.rst

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Available Functions
2727

2828
The following functions are available:
2929

30-
.. php:function:: number_to_size($num[, $precision = 1[, $locale = null])
30+
.. php:function:: number_to_size($num[, $precision = 1[, $locale = null]])
3131
3232
:param mixed $num: Number of bytes
3333
:param int $precision: Floating point precision
@@ -86,21 +86,23 @@ The following functions are available:
8686

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

89-
.. php:function:: number_to_currency($num, $currency[, $locale = null])
89+
.. php:function:: number_to_currency($num, $currency[, $locale = null[, $fraction = 0]])
9090
91-
:param mixed $num: Number to format
91+
:param float $num: Number to format
9292
:param string $currency: The currency type, i.e., USD, EUR, etc
93-
:param string $locale: The locale to use for formatting
93+
:param string|null $locale: The locale to use for formatting
9494
:param integer $fraction: Number of fraction digits after decimal point
9595
:returns: The number as the appropriate currency for the locale
9696
:rtype: string
9797

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

100-
echo number_to_currency(1234.56, 'USD'); // Returns $1,234.56
101-
echo number_to_currency(1234.56, 'EUR'); // Returns €1,234.56
102-
echo number_to_currency(1234.56, 'GBP'); // Returns £1,234.56
103-
echo number_to_currency(1234.56, 'YEN'); // Returns YEN1,234.56
100+
echo number_to_currency(1234.56, 'USD', 'en_US', 2); // Returns $1,234.56
101+
echo number_to_currency(1234.56, 'EUR', 'de_DE', 2); // Returns 1.234,56 €
102+
echo number_to_currency(1234.56, 'GBP', 'en_GB', 2); // Returns £1,234.56
103+
echo number_to_currency(1234.56, 'YEN', 'ja_JP', 2); // Returns YEN 1,234.56
104+
105+
If you don't specify a locale, the Request locale is used.
104106

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

0 commit comments

Comments
 (0)