Skip to content

Commit 3e03614

Browse files
committed
number_to_currency implemented
1 parent 646f458 commit 3e03614

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ We are not looking for out-of-scope contributions, only those that would be cons
6161
Please read the *Contributing to CodeIgniter* section in the user guide
6262

6363
## Server Requirements
64-
PHP version 7 or higher is required.
64+
PHP version 7 or higher is required, with the following extensions installed:
65+
66+
- intl
67+
6568

6669
## Running CodeIgniter Tests
6770
Information on running CodeIgniter test suite can be found in the [README.md](tests/README.md) file in the tests directory.

system/Helpers/number_helper.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,32 @@ function number_to_amount($num, int $precision, string $locale = null)
152152

153153
//--------------------------------------------------------------------
154154

155+
if (! function_exists('number_to_currency'))
156+
{
157+
function number_to_currency($num, string $currency = null, string $locale = null)
158+
{
159+
return format_number($num, 1, $locale, [
160+
'type' => NumberFormatter::CURRENCY,
161+
'currency' => $currency
162+
]);
163+
}
164+
}
165+
166+
//--------------------------------------------------------------------
167+
155168
if (! function_exists('format_number'))
156169
{
170+
/**
171+
* A general purpose, locale-aware, number_format method.
172+
* Used by all of the functions of the number_helper.
173+
*
174+
* @param $num
175+
* @param int $precision
176+
* @param string|null $locale
177+
* @param array $options
178+
*
179+
* @return string
180+
*/
157181
function format_number($num, int $precision = 1, string $locale = null, array $options=[])
158182
{
159183
// Locale is either passed in here, negotiated with client, or grabbed from our config file.
@@ -169,10 +193,17 @@ function format_number($num, int $precision = 1, string $locale = null, array $o
169193
$pattern = '#,##0.'. str_repeat('#', $precision);
170194

171195
$formatter = new NumberFormatter($locale, $type);
172-
$formatter->setPattern($pattern);
173196

174197
// Try to format it per the locale
175-
$output = $formatter->format($num);
198+
if ($type == NumberFormatter::CURRENCY)
199+
{
200+
$output = $formatter->formatCurrency($num, $options['currency']);
201+
}
202+
else
203+
{
204+
$formatter->setPattern($pattern);
205+
$output = $formatter->format($num);
206+
}
176207

177208
// This might lead a trailing period if $precision == 0
178209
$output = trim($output, '. ');

tests/system/Helpers/NumberHelperTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,14 @@ public function test_quadrillions()
8181
{
8282
$this->assertEquals('123.5 quadrillion', number_to_amount('123,456,700,000,000,000', 1, 'en_US'));
8383
}
84+
85+
/**
86+
* @group single
87+
*/
88+
public function test_currency_current_locale()
89+
{
90+
$this->assertEquals('$1,234.56', number_to_currency(1234.56, 'USD', 'en_US'));
91+
$this->assertEquals('£1,234.56', number_to_currency(1234.56, 'GBP', 'en_GB'));
92+
}
93+
8494
}

0 commit comments

Comments
 (0)