yii\i18n\Formatter::asScientific PHP Метод

asScientific() публичный Метод

Formats the value as a scientific number.
public asScientific ( 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.
$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]].
Результат string the formatted result.
    public function asScientific($value, $decimals = null, $options = [], $textOptions = [])
    {
        if ($value === null) {
            return $this->nullDisplay;
        }
        $value = $this->normalizeNumericValue($value);
        if ($this->_intlLoaded) {
            $f = $this->createNumberFormatter(NumberFormatter::SCIENTIFIC, $decimals, $options, $textOptions);
            if (($result = $f->format($value)) === false) {
                throw new InvalidParamException('Formatting scientific number value failed: ' . $f->getErrorCode() . ' ' . $f->getErrorMessage());
            }
            return $result;
        } else {
            if ($decimals !== null) {
                return sprintf("%.{$decimals}E", $value);
            } else {
                return sprintf('%.E', $value);
            }
        }
    }

Usage Example

Пример #1
0
 public function testAsScientific()
 {
     $value = '123';
     $this->assertSame('1.23E2', $this->formatter->asScientific($value));
     $value = '123456';
     $this->assertSame("1.23456E5", $this->formatter->asScientific($value));
     $value = '-123456.123';
     $this->assertSame("-1.23456123E5", $this->formatter->asScientific($value));
     $this->assertSame($this->formatter->nullDisplay, $this->formatter->asScientific(null));
 }
All Usage Examples Of yii\i18n\Formatter::asScientific