ICanBoogie/I18n master
  • Namespace
  • Class

Namespaces

  • ICanBoogie
    • I18n
      • Translator

Classes

  • FormattedString
  • Helpers
  • Locale
  • NumberFormatter
  • Translator

Functions

  • date_period
  • format_currency
  • format_date
  • format_datetime
  • format_number
  • format_size
  • format_time
  • get_cldr
  • get_language
  • get_locale
  • set_locale
  • t

Class NumberFormatter

NumberFormatter provides number localization features.

NumberFormatter formats a number (integer or float) and outputs a string based on the specified format. A NumberFormatter instance is associated with a locale, and thus generates the string representation of the number in a locale-dependent fashion.

NumberFormatter currently supports currency format, percentage format, decimal format, and custom format. The first three formats are specified in the locale data, while the custom format allows you to enter an arbitrary format string.

A format string may consist of the following special characters:

  • dot (.): the decimal point. It will be replaced with the localized decimal point.
  • comma (,): the grouping separator. It will be replaced with the localized grouping separator.
  • zero (0): required digit. This specifies the places where a digit must appear (will pad 0 if not).
  • hash (#): optional digit. This is mainly used to specify the location of decimal point and grouping separators.
  • currency (¤): the currency placeholder. It will be replaced with the localized currency symbol.
  • percentage (%): the percetage mark. If appearing, the number will be multiplied by 100 before being formatted.
  • permillage (‰): the permillage mark. If appearing, the number will be multiplied by 1000 before being formatted.
  • semicolon (;): the character separating positive and negative number sub-patterns.

Anything surrounding the pattern (or sub-patterns) will be kept.

The followings are some examples:

Pattern "#,##0.00" will format 12345.678 as "12,345.68".
Pattern "#,#,#0.00" will format 12345.6 as "1,2,3,45.60".

Note, in the first example, the number is rounded first before applying the formatting. And in the second example, the pattern specifies two grouping sizes.

NumberFormatter attempts to implement number formatting according to the Unicode Technical Standard #35. The following features are NOT implemented:

  • significant digit
  • scientific format
  • arbitrary literal characters
  • arbitrary padding
Namespace: ICanBoogie\I18n
Author: Wei Zhuo <weizhuo[at]gmail[dot]com>
Author: Qiang Xue qiang.xue@gmail.com
Author: Olivier Laviale gmail@olvlvl.com
Located at number-formatter.php

Methods summary

public
# __construct( ICanBoogie\I18n\Locale $locale )

Constructor.

Constructor.

Parameters

$locale
Locale instance.
public string
# format( string $value, mixed $pattern = null, string $currency = null )

Formats a number based on the specified pattern. Note, if the format contains '%', the number will be multiplied by 100 first. If the format contains '‰', the number will be multiplied by 1000. If the format contains currency placeholder, it will be replaced by the specified localized currency symbol.

Formats a number based on the specified pattern. Note, if the format contains '%', the number will be multiplied by 100 first. If the format contains '‰', the number will be multiplied by 1000. If the format contains currency placeholder, it will be replaced by the specified localized currency symbol.

Parameters

$value
$pattern format pattern
$pattern
$value the number to be formatted
$currency

3-letter ISO 4217 code. For example, the code "USD" represents the US Dollar and "EUR" represents the Euro currency. The currency placeholder in the pattern will be replaced with the currency symbol. If null, no replacement will be done.

Returns

string
the formatting result.
public string
# format_currency( mixed $value, string $currency )

Formats a number using the currency format defined in the locale.

Formats a number using the currency format defined in the locale.

Parameters

$value
the number to be formatted
$currency

3-letter ISO 4217 code. For example, the code "USD" represents the US Dollar and "EUR" represents the Euro currency. The currency placeholder in the pattern will be replaced with the currency symbol.

Returns

string
the formatting result.
public string
# format_percentage( mixed $value )

Formats a number using the percentage format defined in the locale. Note, if the percentage format contains '%', the number will be multiplied by 100 first. If the percentage format contains '‰', the number will be multiplied by 1000.

Formats a number using the percentage format defined in the locale. Note, if the percentage format contains '%', the number will be multiplied by 100 first. If the percentage format contains '‰', the number will be multiplied by 1000.

Parameters

$value
the number to be formatted

Returns

string
the formatting result.
public string
# format_decimal( mixed $value )

Formats a number using the decimal format defined in the locale.

Formats a number using the decimal format defined in the locale.

Parameters

$value
the number to be formatted

Returns

string
the formatting result.
protected string
# format_number( array $value, mixed $format )

Formats a number based on a format. This is the method that does actual number formatting.

Formats a number based on a format. This is the method that does actual number formatting.

Parameters

$value

$format format with the following structure:

array(
    'decimalDigits'=>2,     // number of required digits after decimal point; 0s will be padded if not enough digits; if -1, it means we should drop decimal point
 'maxDecimalDigits'=>3,  // maximum number of digits after decimal point. Additional digits will be truncated.
    'integerDigits'=>1,     // number of required digits before decimal point; 0s will be padded if not enough digits
    'groupSize1'=>3,        // the primary grouping size; if 0, it means no grouping
    'groupSize2'=>0,        // the secondary grouping size; if 0, it means no secondary grouping
    'positivePrefix'=>'+',  // prefix to positive number
    'positiveSuffix'=>'',   // suffix to positive number
    'negativePrefix'=>'(',  // prefix to negative number
    'negativeSuffix'=>')',  // suffix to negative number
    'multiplier'=>1,        // 100 for percent, 1000 for per mille
);
$format
$value the number to be formatted

Returns

string
the formatted result
protected array
# parse_format( string $pattern )

Parses a given string pattern.

Parses a given string pattern.

Parameters

$pattern
the pattern to be parsed

Returns

array
the parsed pattern

See

format_number

Properties summary

protected ICanBoogie\I18n\Locale $locale

Locale.

Locale.

#
protected array $conventions

Shortcut to the locale numbers convention.

Shortcut to the locale numbers convention.

#
ICanBoogie/I18n master API documentation generated by ApiGen