asCurrency() public method

This function does not require the PHP intl extension to be installed to work, but it is highly recommended to install it to get good formatting results.
public asCurrency ( mixed $value, string $currency = null, array $options = [], array $textOptions = [] ) : string
$value mixed the value to be formatted.
$currency string the 3-letter ISO 4217 currency code indicating the currency to use. If null, [[currencyCode]] will be used.
$options array optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]].
$textOptions array optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]].
return string the formatted result.
示例#1
0
 public function testAsCurrency()
 {
     $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
     // 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->assertSame($this->formatter->nullDisplay, $this->formatter->asCurrency(null));
 }
示例#2
0
 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));
 }
示例#3
0
 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));
     // empty input
     $this->formatter->currencyCode = 'USD';
     $this->formatter->numberFormatterSymbols = [];
     $this->assertSame("USD 0.00", $this->formatter->asCurrency(false));
     $this->assertSame("USD 0.00", $this->formatter->asCurrency(""));
     // null display
     $this->assertSame($this->formatter->nullDisplay, $this->formatter->asCurrency(null));
 }
示例#4
0
 public function asCurrency($value, $currency = null, $options = [], $textOptions = [])
 {
     if ($this->currencyFormat === null) {
         return parent::asCurrency($value, $currency, $options, $textOptions);
     }
     return strtr($this->currencyFormat, ['{value}' => $this->asDecimal($value, 2, $options, $textOptions), '{currency}' => $currency]);
 }