|
The NumberFormatter classIntroductionPrograms store and operate on numbers using a locale-independent binary representation. When displaying or printing a number it is converted to a locale-specific string. For example, the number 12345.67 is "12,345.67" in the US, "12 345,67" in France and "12.345,67" in Germany. By invoking the methods provided by the NumberFormatter class, you can format numbers, currencies, and percentages according to the specified or default locale. NumberFormatter is locale-sensitive so you need to create a new NumberFormatter for each locale. NumberFormatter methods format primitive-type numbers, such as double and output the number as a locale-specific string. For currencies you can use currency format type to create a formatter that returns a string with the formatted number and the appropriate currency sign. Of course, the NumberFormatter class is unaware of exchange rates so, the number output is the same regardless of the specified currency. This means that the same number has different monetary values depending on the currency locale. If the number is 9988776.65 the results will be:
In order to format percentages, create a locale-specific formatter with percentage format type. With this formatter, a decimal fraction such as 0.75 is displayed as 75%. For more complex formatting, like spelled-out numbers, the rule-based number formatters are used. Class synopsis
NumberFormatter
/* Constants */
public
const
int
NumberFormatter::PATTERN_DECIMAL;
public
const
int
NumberFormatter::DECIMAL;
public
const
int
NumberFormatter::CURRENCY;
public
const
int
NumberFormatter::PERCENT;
public
const
int
NumberFormatter::SCIENTIFIC;
public
const
int
NumberFormatter::SPELLOUT;
public
const
int
NumberFormatter::ORDINAL;
public
const
int
NumberFormatter::DURATION;
public
const
int
NumberFormatter::PATTERN_RULEBASED;
public
const
int
NumberFormatter::IGNORE;
public
const
int
NumberFormatter::CURRENCY_ACCOUNTING;
public
const
int
NumberFormatter::DEFAULT_STYLE;
public
const
int
NumberFormatter::ROUND_CEILING;
public
const
int
NumberFormatter::ROUND_FLOOR;
public
const
int
NumberFormatter::ROUND_DOWN;
public
const
int
NumberFormatter::ROUND_UP;
public
const
int
NumberFormatter::ROUND_HALFEVEN;
public
const
int
NumberFormatter::ROUND_HALFDOWN;
public
const
int
NumberFormatter::ROUND_HALFUP;
public
const
int
NumberFormatter::PAD_BEFORE_PREFIX;
public
const
int
NumberFormatter::PAD_AFTER_PREFIX;
public
const
int
NumberFormatter::PAD_BEFORE_SUFFIX;
public
const
int
NumberFormatter::PAD_AFTER_SUFFIX;
public
const
int
NumberFormatter::PARSE_INT_ONLY;
public
const
int
NumberFormatter::GROUPING_USED;
public
const
int
NumberFormatter::DECIMAL_ALWAYS_SHOWN;
public
const
int
NumberFormatter::MAX_INTEGER_DIGITS;
public
const
int
NumberFormatter::MIN_INTEGER_DIGITS;
public
const
int
NumberFormatter::INTEGER_DIGITS;
public
const
int
NumberFormatter::MAX_FRACTION_DIGITS;
public
const
int
NumberFormatter::MIN_FRACTION_DIGITS;
public
const
int
NumberFormatter::FRACTION_DIGITS;
public
const
int
NumberFormatter::MULTIPLIER;
public
const
int
NumberFormatter::GROUPING_SIZE;
public
const
int
NumberFormatter::ROUNDING_MODE;
public
const
int
NumberFormatter::ROUNDING_INCREMENT;
public
const
int
NumberFormatter::FORMAT_WIDTH;
public
const
int
NumberFormatter::PADDING_POSITION;
public
const
int
NumberFormatter::SECONDARY_GROUPING_SIZE;
public
const
int
NumberFormatter::SIGNIFICANT_DIGITS_USED;
public
const
int
NumberFormatter::MIN_SIGNIFICANT_DIGITS;
public
const
int
NumberFormatter::MAX_SIGNIFICANT_DIGITS;
public
const
int
NumberFormatter::LENIENT_PARSE;
public
const
int
NumberFormatter::POSITIVE_PREFIX;
public
const
int
NumberFormatter::POSITIVE_SUFFIX;
public
const
int
NumberFormatter::NEGATIVE_PREFIX;
public
const
int
NumberFormatter::NEGATIVE_SUFFIX;
public
const
int
NumberFormatter::PADDING_CHARACTER;
public
const
int
NumberFormatter::CURRENCY_CODE;
public
const
int
NumberFormatter::DEFAULT_RULESET;
public
const
int
NumberFormatter::PUBLIC_RULESETS;
public
const
int
NumberFormatter::DECIMAL_SEPARATOR_SYMBOL;
public
const
int
NumberFormatter::GROUPING_SEPARATOR_SYMBOL;
public
const
int
NumberFormatter::PATTERN_SEPARATOR_SYMBOL;
public
const
int
NumberFormatter::PERCENT_SYMBOL;
public
const
int
NumberFormatter::ZERO_DIGIT_SYMBOL;
public
const
int
NumberFormatter::DIGIT_SYMBOL;
public
const
int
NumberFormatter::MINUS_SIGN_SYMBOL;
public
const
int
NumberFormatter::PLUS_SIGN_SYMBOL;
public
const
int
NumberFormatter::CURRENCY_SYMBOL;
public
const
int
NumberFormatter::INTL_CURRENCY_SYMBOL;
public
const
int
NumberFormatter::MONETARY_SEPARATOR_SYMBOL;
public
const
int
NumberFormatter::EXPONENTIAL_SYMBOL;
public
const
int
NumberFormatter::PERMILL_SYMBOL;
public
const
int
NumberFormatter::PAD_ESCAPE_SYMBOL;
public
const
int
NumberFormatter::INFINITY_SYMBOL;
public
const
int
NumberFormatter::NAN_SYMBOL;
public
const
int
NumberFormatter::SIGNIFICANT_DIGIT_SYMBOL;
public
const
int
NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL;
public
const
int
NumberFormatter::TYPE_DEFAULT;
public
const
int
NumberFormatter::TYPE_INT32;
public
const
int
NumberFormatter::TYPE_INT64;
public
const
int
NumberFormatter::TYPE_DOUBLE;
public
const
int
NumberFormatter::TYPE_CURRENCY;
/* Methods */
public __construct(string
$locale , int $style , stringnull $pattern = null )public static NumberFormatternull create(string
$locale , int $style , stringnull $pattern = null )public stringfalse format(intfloat
$num , int $type = NumberFormatter::TYPE_DEFAULT)public stringfalse formatCurrency(float
$amount , string $currency )public intfloatfalse getAttribute(int
$attribute )public int getErrorCode()
public string getErrorMessage()
public stringfalse getLocale(int
$type = ULOC_ACTUAL_LOCALE )public stringfalse getPattern()
public stringfalse getSymbol(int
$symbol )public stringfalse getTextAttribute(int
$attribute )public intfloatfalse parse(string
$string , int $type = NumberFormatter::TYPE_DOUBLE, int &$offset = null )public floatfalse parseCurrency(string
$string , string &$currency , int &$offset = null )public bool setAttribute(int
$attribute , intfloat $value )public bool setPattern(string
$pattern )public bool setSymbol(int
$symbol , string $value )public bool setTextAttribute(int
$attribute , string $value )Predefined ConstantsFormat TypesThese styles are used by the numfmt_create to define the type of the formatter.
Number Format SpecifiersThese constants define how the numbers are parsed or formatted. They should be used as arguments to numfmt_format and numfmt_parse.
Number Format AttributesNumber format attribute used by numfmt_get_attribute and numfmt_set_attribute.
Number Format Text AttributesNumber format text attribute used by numfmt_get_text_attribute and numfmt_set_text_attribute.
Symbol Format SpecifiersNumber format symbols used by numfmt_get_symbol and numfmt_set_symbol.
Rounding Modes
Rounding mode values used by numfmt_get_attribute
and numfmt_set_attribute with
Padding Specifiers
Pad position values used by numfmt_get_attribute
and numfmt_set_attribute with
|