yii\i18n\Formatter::asInteger PHP Method

asInteger() public method

Formats the value as an integer number by removing any decimal digits without rounding.
public asInteger ( mixed $value, array $options = [], array $textOptions = [] ) : string
$value mixed the value to be formatted.
$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 asInteger($value, $options = [], $textOptions = [])
    {
        if ($value === null) {
            return $this->nullDisplay;
        }
        $value = $this->normalizeNumericValue($value);
        if ($this->_intlLoaded) {
            $f = $this->createNumberFormatter(NumberFormatter::DECIMAL, null, $options, $textOptions);
            $f->setAttribute(NumberFormatter::FRACTION_DIGITS, 0);
            if (($result = $f->format($value, NumberFormatter::TYPE_INT64)) === false) {
                throw new InvalidParamException('Formatting integer value failed: ' . $f->getErrorCode() . ' ' . $f->getErrorMessage());
            }
            return $result;
        } else {
            return number_format((int) $value, 0, $this->decimalSeparator, $this->thousandSeparator);
        }
    }

Usage Example

 public function testAsScientific()
 {
     $value = '123';
     $this->assertSame('1.23E+2', $this->formatter->asScientific($value, 2));
     $value = '123456';
     $this->assertSame("1.234560E+5", $this->formatter->asScientific($value));
     $value = '-123456.123';
     $this->assertSame("-1.234561E+5", $this->formatter->asScientific($value));
     // empty input
     $this->assertSame("0", $this->formatter->asInteger(false));
     $this->assertSame("0", $this->formatter->asInteger(""));
     // null display
     $this->assertSame($this->formatter->nullDisplay, $this->formatter->asScientific(null));
 }
All Usage Examples Of yii\i18n\Formatter::asInteger