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
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
format( string $value
, mixed $pattern
= null, string $currency
= null )
: string
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.
public
format_currency( mixed $value
, string $currency
)
: string
Formats a number using the currency format defined in the locale.
public
format_percentage( mixed $value
)
: string
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.
public
format_decimal( mixed $value
)
: string
Formats a number using the decimal format defined in the locale.