Commit 8d397e7f by Carsten Brandt

finished currency formatter

parent 98079b84
...@@ -144,8 +144,7 @@ class Formatter extends Component ...@@ -144,8 +144,7 @@ class Formatter extends Component
*/ */
public $numberFormatterTextOptions = []; public $numberFormatterTextOptions = [];
/** /**
* @var string the international currency code displayed when formatting a number. * @var string the 3-letter ISO 4217 currency code indicating the default currency to use for [[asCurrency]].
* If not set, the currency code corresponding to [[locale]] will be used. TODO default value?
*/ */
public $currencyCode; public $currencyCode;
/** /**
...@@ -958,6 +957,7 @@ class Formatter extends Component ...@@ -958,6 +957,7 @@ class Formatter extends Component
/** /**
* Formats the value as a scientific number. * Formats the value as a scientific number.
*
* @param mixed $value the value to be formatted. * @param mixed $value the value to be formatted.
* @param integer $decimals the number of digits after the decimal point. * @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]]. * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]].
...@@ -986,28 +986,36 @@ class Formatter extends Component ...@@ -986,28 +986,36 @@ class Formatter extends Component
/** /**
* Formats the value as a currency number. * 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 mixed $value the value to be formatted.
* @param string $currency the 3-letter ISO 4217 currency code indicating the currency to use. * @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 $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]]. * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]].
* @return string the formatted result. * @return string the formatted result.
* @throws InvalidParamException if the input value is not numeric. * @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 = []) public function asCurrency($value, $currency = null, $options = [], $textOptions = [])
{ {
if ($value === null) { if ($value === null) {
return $this->nullDisplay; 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 ($currency === null) {
if ($this->currencyCode === null) {
throw new InvalidConfigException('The default currency code for the formatter is not defined.');
}
$currency = $this->currencyCode; $currency = $this->currencyCode;
} }
if ($this->_intlLoaded) { if ($this->_intlLoaded) {
$formatter = $this->createNumberFormatter(NumberFormatter::CURRENCY, null, $options, $textOptions); $formatter = $this->createNumberFormatter(NumberFormatter::CURRENCY, null, $options, $textOptions);
return $formatter->formatCurrency($value, $currency); return $formatter->formatCurrency($value, $currency);
} else { } else {
return $currency . ' ' . $this->asDecimal($value, null, $options, $textOptions); // TODO be compatible to intl return $currency . ' ' . $this->asDecimal($value, 2, $options, $textOptions);
} }
} }
......
...@@ -483,20 +483,40 @@ class FormatterTest extends TestCase ...@@ -483,20 +483,40 @@ class FormatterTest extends TestCase
public function testIntlAsCurrency() public function testIntlAsCurrency()
{ {
$this->testAsCurrency(); $this->formatter->locale = 'en_US';
} $this->formatter->currencyCode = 'USD';
$this->assertSame('$123.00', $this->formatter->asCurrency('123'));
public function testAsCurrency() $this->assertSame('$123,456.00', $this->formatter->asCurrency('123456'));
{ $this->assertSame('$0.00', $this->formatter->asCurrency('0'));
$value = '123';
$this->assertSame('$123.00', $this->formatter->asCurrency($value));
$value = '123.456';
$this->assertSame("$123.46", $this->formatter->asCurrency($value));
// Starting from ICU 52.1, negative currency value will be formatted as -$123,456.12 // 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 // see: http://source.icu-project.org/repos/icu/icu/tags/release-52-1/source/data/locales/en.txt
// $value = '-123456.123'; // $value = '-123456.123';
// $this->assertSame("($123,456.12)", $this->formatter->asCurrency($value)); // $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 // null display
$this->assertSame($this->formatter->nullDisplay, $this->formatter->asCurrency(null)); $this->assertSame($this->formatter->nullDisplay, $this->formatter->asCurrency(null));
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment