mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
Merge pull request #2635 from najdanovicivan/helper-number-currency
Number Helper - Added Fraction parameter in number_to_currency method
This commit is contained in:
commit
618cf80a8b
@ -179,17 +179,19 @@ if (! function_exists('number_to_amount'))
|
||||
if (! function_exists('number_to_currency'))
|
||||
{
|
||||
/**
|
||||
* @param float $num
|
||||
* @param string $currency
|
||||
* @param string $locale
|
||||
* @param float $num
|
||||
* @param string $currency
|
||||
* @param string $locale
|
||||
* @param integer $fraction
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function number_to_currency(float $num, string $currency, string $locale = null): string
|
||||
function number_to_currency(float $num, string $currency, string $locale = null, int $fraction = null): string
|
||||
{
|
||||
return format_number($num, 1, $locale, [
|
||||
'type' => NumberFormatter::CURRENCY,
|
||||
'currency' => $currency,
|
||||
'fraction' => $fraction,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@ -217,19 +219,20 @@ if (! function_exists('format_number'))
|
||||
// Type can be any of the NumberFormatter options, but provide a default.
|
||||
$type = (int) ($options['type'] ?? NumberFormatter::DECIMAL);
|
||||
|
||||
// In order to specify a precision, we'll have to modify
|
||||
// the pattern used by NumberFormatter.
|
||||
$pattern = '#,##0.' . str_repeat('#', $precision);
|
||||
|
||||
$formatter = new NumberFormatter($locale, $type);
|
||||
|
||||
// Try to format it per the locale
|
||||
if ($type === NumberFormatter::CURRENCY)
|
||||
{
|
||||
$formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $options['fraction']);
|
||||
$output = $formatter->formatCurrency($num, $options['currency']);
|
||||
}
|
||||
else
|
||||
{
|
||||
// In order to specify a precision, we'll have to modify
|
||||
// the pattern used by NumberFormatter.
|
||||
$pattern = '#,##0.' . str_repeat('#', $precision);
|
||||
|
||||
$formatter->setPattern($pattern);
|
||||
$output = $formatter->format($num);
|
||||
}
|
||||
|
@ -256,16 +256,18 @@ class Filters
|
||||
* @param $value
|
||||
* @param string $currency
|
||||
* @param string|null $locale
|
||||
* @param integer $fraction
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function local_currency($value, string $currency, string $locale = null): string
|
||||
public static function local_currency($value, string $currency, string $locale = null, $fraction = null): string
|
||||
{
|
||||
helper('number');
|
||||
|
||||
$options = [
|
||||
'type' => NumberFormatter::CURRENCY,
|
||||
'currency' => $currency,
|
||||
'fraction' => $fraction,
|
||||
];
|
||||
|
||||
return format_number($value, 2, $locale, $options);
|
||||
|
@ -112,8 +112,9 @@ final class NumberHelperTest extends \CodeIgniter\Test\CIUnitTestCase
|
||||
*/
|
||||
public function testCurrencyCurrentLocale()
|
||||
{
|
||||
$this->assertEquals('$1,234.56', number_to_currency(1234.56, 'USD', 'en_US'));
|
||||
$this->assertEquals('£1,234.56', number_to_currency(1234.56, 'GBP', 'en_GB'));
|
||||
$this->assertEquals('$1,234.56', number_to_currency(1234.56, 'USD', 'en_US', 2));
|
||||
$this->assertEquals('£1,234.56', number_to_currency(1234.56, 'GBP', 'en_GB', 2));
|
||||
$this->assertEquals('1.234,56 RSD', number_to_currency(1234.56, 'RSD', 'sr_RS', 2));
|
||||
}
|
||||
|
||||
public function testNumbersThatArent()
|
||||
|
@ -403,7 +403,7 @@ EOF;
|
||||
'mynum' => 1234567.891234567890000,
|
||||
];
|
||||
|
||||
$template = '{ mynum|local_currency(EUR,de_DE) }';
|
||||
$template = '{ mynum|local_currency(EUR,de_DE,2) }';
|
||||
|
||||
$parser->setData($data);
|
||||
$this->assertEquals('1.234.567,89 €', $parser->renderString($template));
|
||||
|
@ -95,13 +95,14 @@ The following functions are available:
|
||||
:param mixed $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 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, '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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user