yii\i18n\Formatter::asDecimal PHP Method

asDecimal() public method

Property [[decimalSeparator]] will be used to represent the decimal point. The value is rounded automatically to the defined decimal digits.
See also: decimalSeparator
See also: thousandSeparator
public asDecimal ( mixed $value, integer $decimals = null, array $options = [], array $textOptions = [] ) : string
$value mixed the value to be formatted.
$decimals integer the number of digits after the decimal point. If not given the number of digits is determined from the [[locale]] and if the [PHP intl extension](http://php.net/manual/en/book.intl.php) is not available defaults to `2`.
$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.
    public function asDecimal($value, $decimals = null, $options = [], $textOptions = [])
    {
        if ($value === null) {
            return $this->nullDisplay;
        }
        $value = $this->normalizeNumericValue($value);
        if ($this->_intlLoaded) {
            $f = $this->createNumberFormatter(NumberFormatter::DECIMAL, $decimals, $options, $textOptions);
            if (($result = $f->format($value)) === false) {
                throw new InvalidParamException('Formatting decimal value failed: ' . $f->getErrorCode() . ' ' . $f->getErrorMessage());
            }
            return $result;
        } else {
            if ($decimals === null) {
                $decimals = 2;
            }
            return number_format($value, $decimals, $this->decimalSeparator, $this->thousandSeparator);
        }
    }

Usage Example

 public function testAsDecimal()
 {
     $value = '123';
     $this->assertSame($value, $this->formatter->asDecimal($value));
     $value = '123456';
     $this->assertSame("123,456", $this->formatter->asDecimal($value));
     $value = '-123456.123';
     $this->assertSame("-123,456.123", $this->formatter->asDecimal($value));
     $this->assertSame($this->formatter->nullDisplay, $this->formatter->asDecimal(null));
 }
All Usage Examples Of yii\i18n\Formatter::asDecimal