ExpressionEngine® User Guide

Number Formatter

The Number Formatter is part of the Format Service and handles many common formatting needs for numeric content.

Usage

Bytes

$display_size = ee('Format')->make('Number', 76752)->bytes();
// 75<abbr title="Kilobytes">KB</abbr>

$display_size = ee('Format')->make('Number', 76752)->bytes(FALSE);
// 75 kilobytes

Currency

$money = ee('Format')->make('Number', 4736234.5)->currency();
// $4,736,234.58 (presuming default / US locale in the PHP environment)

$money = ee('Format')->make('Number', 4736234.58)->currency(['locale' => 'de_DE', 'currency' => 'EUR']);
// 4.736.234,58 €

Warning

For the greatest accuracy, the PHP intl extension must be available (PHP’s default). Without it, the currency symbol may be placed in the wrong position for non-US locales. The fallback also relies on strfmon which is not available on all systems, such as Windows.

Duration

$duration = ee('Format')->make('Number', 112358)->duration();
// 31:12:38

Ordinal

$ordinal = ee('Format')->make('Number', 43)->ordinal();
// 43rd

Spellout

$written_check = ee('Format')->make('Number', 112358.13)->spellout();
// one hundred twelve thousand three hundred fifty-eight point one three

$written_check = ee('Format')->make('Number', 112358.13)->spellout(['capitalize' => 'ucwords']);
// One Hundred Twelve Thousand Three Hundred Fifty-eight Point One Three

$written_check = ee('Format')->make('Number', 112358.13)->spellout(['locale' => 'de_DE']);
// ein­hundert­zwölf­tausend­drei­hundert­acht­und­fünfzig Komma eins drei

Warning

This method requires the PHP intl extension (enabled by default).

API Reference

class EllisLab\ExpressionEngine\Service\Formatter\Formats\Number
EllisLab\ExpressionEngine\Service\Formatter\Formats\Number::bytes($abbr = TRUE, $include_markup = TRUE)

Formats a binary byte multiple into a human-readable measure of units, e.g. B, KB, MB, GB.

Parameters:
  • $abbr (bool) – (default: TRUE) Use the abbreviated form of the byte format
  • $include_markup (bool) – (default: TRUE) Output with <abbr> HTML. Only affects abbreviated forms.
Returns:

A Formatter object

Return type:

object

EllisLab\ExpressionEngine\Service\Formatter\Formats\Number::currency($options = [])

Formats as currency. Greatest accuracy requires the PHP intl extension to be available

Parameters:
  • $options (array) –
    • (string) currency code (USD, EUR, etc.)
    • (string) decimals decimal precision (default based on locale)
    • (string) locale (default: en_US.UTF-8)
Returns:

A Formatter object

Return type:

object

EllisLab\ExpressionEngine\Service\Formatter\Formats\Number::duration($options = [])

Formats as a duration using a rule-based format, e.g.: hh:mm:ss, mm:ss, or ss sec.

Parameters:
  • $options (array) –
    • (string) locale (default: en_US.UTF-8)
Returns:

A Formatter object

Return type:

object

EllisLab\ExpressionEngine\Service\Formatter\Formats\Number::ordinal($options = [])

Formats with an ordinal suffix, e.g. 127th. Locales other than English require the PHP intl extension.

Parameters:
  • $options (array) –
    • (string) locale (default: en_US.UTF-8)
Returns:

A Formatter object

Return type:

object

EllisLab\ExpressionEngine\Service\Formatter\Formats\Number::spellout($options = [])

Spell out the number as words. Requires the PHP intl extension.

Parameters:
  • $options (array) –
    • (string) capitalize ‘ucfirst’ or ‘ucwords’
    • (string) locale (default: en_US.UTF-8)
Returns:

A Formatter object

Return type:

object