Class Number
Number helper library.
Methods to make numbers more readable.
Constants
Property Summary
- 
        $_defaultCurrency protected staticstring|nullDefault currency used by Number::currency() 
- 
        $_formatters protected staticarrayA list of number formatters indexed by locale and type 
Method Summary
- 
          _setAttributes() protected staticSet formatter attributes 
- 
          config() public staticConfigure formatters. 
- 
          currency() public staticFormats a number into a currency format. 
- 
          defaultCurrency() public staticGetter/setter for default currency 
- 
          format() public staticFormats a number into the correct locale format 
- 
          formatDelta() public staticFormats a number into the correct locale format to show deltas (signed differences in value). 
- 
          formatter() public staticReturns a formatter object that can be reused for similar formatting task under the same locale and options. This is often a speedier alternative to using other methods in this class as only one formatter object needs to be constructed. 
- 
          ordinal() public staticReturns a formatted integer as an ordinal number string (e.g. 1st, 2nd, 3rd, 4th, [...]) 
- 
          parseFloat() public staticParse a localized numeric string and transform it in a float point 
- 
          precision() public staticFormats a number with a level of precision. 
- 
          toPercentage() public staticFormats a number into a percentage string. 
- 
          toReadableSize() public staticReturns a formatted-for-humans file size. 
Method Detail
_setAttributes() ¶ protected static
_setAttributes(NumberFormatter $formatter, array $options = []): NumberFormatterSet formatter attributes
Parameters
- 
                NumberFormatter$formatter
- Number formatter instance. 
- 
                array$options optional
- See Number::formatter() for possible options. 
Returns
NumberFormatterconfig() ¶ public static
config(string $locale, int $type = NumberFormatter::DECIMAL, array $options = []): voidConfigure formatters.
Parameters
- 
                string$locale
- The locale name to use for formatting the number, e.g. fr_FR 
- 
                int$type optional
- The formatter type to construct. Defaults to NumberFormatter::DECIMAL. 
- 
                array$options optional
- See Number::formatter() for possible options. 
Returns
voidcurrency() ¶ public static
currency(float $value, string|null $currency = null, array $options = []): stringFormats a number into a currency format.
Options
- locale- The locale name to use for formatting the number, e.g. fr_FR
- fractionSymbol- The currency symbol to use for fractional numbers.
- fractionPosition- The position the fraction symbol should be placed valid options are 'before' & 'after'.
- before- Text to display before the rendered number
- after- Text to display after the rendered number
- zero- The text to use for zero values, can be a string or a number. e.g. 0, 'Free!'
- places- Number of decimal places to use. e.g. 2
- precision- Maximum Number of decimal places to use, e.g. 2
- pattern- An ICU number pattern to use for formatting the number. e.g #,##0.00
- useIntlCode- Whether or not to replace the currency symbol with the international currency code.
Parameters
- 
                float$value
- Value to format. 
- 
                string|null$currency optional
- International currency name such as 'USD', 'EUR', 'JPY', 'CAD' 
- 
                array$options optional
- Options list. 
Returns
stringNumber formatted as a currency.
defaultCurrency() ¶ public static
defaultCurrency(string|false|null $currency = null): string|nullGetter/setter for default currency
Parameters
- 
                string|false|null$currency optional
- Default currency string to be used by currency() if $currency argument is not provided. If boolean false is passed, it will clear the currently stored value 
Returns
string|nullCurrency
format() ¶ public static
format(float $value, array $options = []): stringFormats a number into the correct locale format
Options:
- places- Minimum number or decimals to use, e.g 0
- precision- Maximum Number of decimal places to use, e.g. 2
- pattern- An ICU number pattern to use for formatting the number. e.g #,##0.00
- locale- The locale name to use for formatting the number, e.g. fr_FR
- before- The string to place before whole numbers, e.g. '['
- after- The string to place after decimal numbers, e.g. ']'
Parameters
- 
                float$value
- A floating point number. 
- 
                array$options optional
- An array with options. 
Returns
stringFormatted number
formatDelta() ¶ public static
formatDelta(float $value, array $options = []): stringFormats a number into the correct locale format to show deltas (signed differences in value).
Options
- places- Minimum number or decimals to use, e.g 0
- precision- Maximum Number of decimal places to use, e.g. 2
- locale- The locale name to use for formatting the number, e.g. fr_FR
- before- The string to place before whole numbers, e.g. '['
- after- The string to place after decimal numbers, e.g. ']'
Parameters
- 
                float$value
- A floating point number 
- 
                array$options optional
- Options list. 
Returns
stringformatted delta
formatter() ¶ public static
formatter(array $options = []): NumberFormatterReturns a formatter object that can be reused for similar formatting task under the same locale and options. This is often a speedier alternative to using other methods in this class as only one formatter object needs to be constructed.
Options
- locale- The locale name to use for formatting the number, e.g. fr_FR
- type- The formatter type to construct, set it to- currencyif you need to format numbers representing money or a NumberFormatter constant.
- places- Number of decimal places to use. e.g. 2
- precision- Maximum Number of decimal places to use, e.g. 2
- pattern- An ICU number pattern to use for formatting the number. e.g #,##0.00
- useIntlCode- Whether or not to replace the currency symbol with the international currency code.
Parameters
- 
                array$options optional
- An array with options. 
Returns
NumberFormatterThe configured formatter instance
ordinal() ¶ public static
ordinal(int|float $value, array $options = []): stringReturns a formatted integer as an ordinal number string (e.g. 1st, 2nd, 3rd, 4th, [...])
Options
- type- The formatter type to construct, set it to- currencyif you need to format numbers representing money or a NumberFormatter constant.
For all other options see formatter().
Parameters
- 
                int|float$value
- An integer 
- 
                array$options optional
- An array with options. 
Returns
stringparseFloat() ¶ public static
parseFloat(string $value, array $options = []): floatParse a localized numeric string and transform it in a float point
Options:
- locale- The locale name to use for parsing the number, e.g. fr_FR
- type- The formatter type to construct, set it to- currencyif you need to parse numbers representing money.
Parameters
- 
                string$value
- A numeric string. 
- 
                array$options optional
- An array with options. 
Returns
floatpoint number
precision() ¶ public static
precision(float $value, int $precision = 3, array $options = []): stringFormats a number with a level of precision.
Options:
- locale: The locale name to use for formatting the number, e.g. fr_FR
Parameters
- 
                float$value
- A floating point number. 
- 
                int$precision optional
- The precision of the returned number. 
- 
                array$options optional
- Additional options 
Returns
stringFormatted float.
Links
toPercentage() ¶ public static
toPercentage(float $value, int $precision = 2, array $options = []): stringFormats a number into a percentage string.
Options:
- multiply: Multiply the input value by 100 for decimal percentages.
- locale: The locale name to use for formatting the number, e.g. fr_FR
Parameters
- 
                float$value
- A floating point number 
- 
                int$precision optional
- The precision of the returned number 
- 
                array$options optional
- Options 
Returns
stringPercentage string
Links
toReadableSize() ¶ public static
toReadableSize(int $size): stringReturns a formatted-for-humans file size.
Parameters
- 
                int$size
- Size in bytes 
Returns
stringHuman readable size
