yii\i18n\Formatter::formatSizeNumber PHP Method

formatSizeNumber() private method

Given the value in bytes formats number part of the human readable form.
private formatSizeNumber ( string | integer | float $value, integer $decimals, array $options, array $textOptions ) : array
$value string | integer | float value in bytes 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]].
return array [parameters for Yii::t containing formatted number, internal position of size unit]
    private function formatSizeNumber($value, $decimals, $options, $textOptions)
    {
        $value = $this->normalizeNumericValue($value);
        $position = 0;
        do {
            if (abs($value) < $this->sizeFormatBase) {
                break;
            }
            $value /= $this->sizeFormatBase;
            $position++;
        } while ($position < 5);
        // no decimals for bytes
        if ($position === 0) {
            $decimals = 0;
        } elseif ($decimals !== null) {
            $value = round($value, $decimals);
        }
        // disable grouping for edge cases like 1023 to get 1023 B instead of 1,023 B
        $oldThousandSeparator = $this->thousandSeparator;
        $this->thousandSeparator = '';
        if ($this->_intlLoaded) {
            $options[NumberFormatter::GROUPING_USED] = false;
        }
        // format the size value
        $params = ['n' => abs($value), 'nFormatted' => $this->asDecimal($value, $decimals, $options, $textOptions)];
        $this->thousandSeparator = $oldThousandSeparator;
        return [$params, $position];
    }