Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
8d397e7f
Commit
8d397e7f
authored
Sep 08, 2014
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
finished currency formatter
parent
98079b84
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
13 deletions
+41
-13
Formatter.php
framework/i18n/Formatter.php
+12
-4
FormatterTest.php
tests/unit/framework/i18n/FormatterTest.php
+29
-9
No files found.
framework/i18n/Formatter.php
View file @
8d397e7f
...
...
@@ -144,8 +144,7 @@ class Formatter extends Component
*/
public
$numberFormatterTextOptions
=
[];
/**
* @var string the international currency code displayed when formatting a number.
* If not set, the currency code corresponding to [[locale]] will be used. TODO default value?
* @var string the 3-letter ISO 4217 currency code indicating the default currency to use for [[asCurrency]].
*/
public
$currencyCode
;
/**
...
...
@@ -958,6 +957,7 @@ class Formatter extends Component
/**
* Formats the value as a scientific number.
*
* @param mixed $value the value to be formatted.
* @param integer $decimals the number of digits after the decimal point.
* @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]].
...
...
@@ -986,28 +986,36 @@ class Formatter extends Component
/**
* Formats the value as a currency number.
*
* This function does not requires the [PHP intl extension](http://php.net/manual/en/book.intl.php) to be installed
* to work but it is highly recommended to install it to get good formatting results.
*
* @param mixed $value the value to be formatted.
* @param string $currency the 3-letter ISO 4217 currency code indicating the currency to use.
* @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]].
* @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]].
* @return string the formatted result.
* @throws InvalidParamException if the input value is not numeric.
* @throws InvalidConfigException if no currency is given and [[currencyCode]] is not defined.
*/
public
function
asCurrency
(
$value
,
$currency
=
null
,
$options
=
[],
$textOptions
=
[])
{
if
(
$value
===
null
)
{
return
$this
->
nullDisplay
;
}
$value
=
$this
->
normalizeNumericValue
(
$value
);
// TODO maybe not a good idea to cast money values?
$value
=
$this
->
normalizeNumericValue
(
$value
);
if
(
$currency
===
null
)
{
if
(
$this
->
currencyCode
===
null
)
{
throw
new
InvalidConfigException
(
'The default currency code for the formatter is not defined.'
);
}
$currency
=
$this
->
currencyCode
;
}
if
(
$this
->
_intlLoaded
)
{
$formatter
=
$this
->
createNumberFormatter
(
NumberFormatter
::
CURRENCY
,
null
,
$options
,
$textOptions
);
return
$formatter
->
formatCurrency
(
$value
,
$currency
);
}
else
{
return
$currency
.
' '
.
$this
->
asDecimal
(
$value
,
null
,
$options
,
$textOptions
);
// TODO be compatible to intl
return
$currency
.
' '
.
$this
->
asDecimal
(
$value
,
2
,
$options
,
$textOptions
);
}
}
...
...
tests/unit/framework/i18n/FormatterTest.php
View file @
8d397e7f
...
...
@@ -483,20 +483,40 @@ class FormatterTest extends TestCase
public
function
testIntlAsCurrency
()
{
$this
->
testAsCurrency
();
}
public
function
testAsCurrency
()
{
$value
=
'123'
;
$this
->
assertSame
(
'$123.00'
,
$this
->
formatter
->
asCurrency
(
$value
));
$value
=
'123.456'
;
$this
->
assertSame
(
"$123.46"
,
$this
->
formatter
->
asCurrency
(
$value
));
$this
->
formatter
->
locale
=
'en_US'
;
$this
->
formatter
->
currencyCode
=
'USD'
;
$this
->
assertSame
(
'$123.00'
,
$this
->
formatter
->
asCurrency
(
'123'
));
$this
->
assertSame
(
'$123,456.00'
,
$this
->
formatter
->
asCurrency
(
'123456'
));
$this
->
assertSame
(
'$0.00'
,
$this
->
formatter
->
asCurrency
(
'0'
));
// Starting from ICU 52.1, negative currency value will be formatted as -$123,456.12
// see: http://source.icu-project.org/repos/icu/icu/tags/release-52-1/source/data/locales/en.txt
// $value = '-123456.123';
// $this->assertSame("($123,456.12)", $this->formatter->asCurrency($value));
$this
->
formatter
->
locale
=
'de_DE'
;
$this
->
formatter
->
currencyCode
=
'USD'
;
$this
->
assertSame
(
'123,00 $'
,
$this
->
formatter
->
asCurrency
(
'123'
));
$this
->
formatter
->
currencyCode
=
'EUR'
;
$this
->
assertSame
(
'123,00 €'
,
$this
->
formatter
->
asCurrency
(
'123'
));
// null display
$this
->
assertSame
(
$this
->
formatter
->
nullDisplay
,
$this
->
formatter
->
asCurrency
(
null
));
}
public
function
testAsCurrency
()
{
$this
->
formatter
->
currencyCode
=
'USD'
;
$this
->
assertSame
(
'USD 123.00'
,
$this
->
formatter
->
asCurrency
(
'123'
));
$this
->
assertSame
(
'USD 0.00'
,
$this
->
formatter
->
asCurrency
(
'0'
));
$this
->
assertSame
(
'USD -123.45'
,
$this
->
formatter
->
asCurrency
(
'-123,45'
));
$this
->
assertSame
(
'USD -123.45'
,
$this
->
formatter
->
asCurrency
(
-
123.45
));
$this
->
formatter
->
currencyCode
=
'EUR'
;
$this
->
assertSame
(
'EUR 123.00'
,
$this
->
formatter
->
asCurrency
(
'123'
));
$this
->
assertSame
(
'EUR 0.00'
,
$this
->
formatter
->
asCurrency
(
'0'
));
$this
->
assertSame
(
'EUR -123.45'
,
$this
->
formatter
->
asCurrency
(
'-123,45'
));
$this
->
assertSame
(
'EUR -123.45'
,
$this
->
formatter
->
asCurrency
(
-
123.45
));
// null display
$this
->
assertSame
(
$this
->
formatter
->
nullDisplay
,
$this
->
formatter
->
asCurrency
(
null
));
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment