NumberFormat class
Formats and parses numbers using locale-sensitive patterns.
This class provides comprehensive and flexible support for a wide variety of localized formats, including <ul> <li><b>Locale-specific symbols</b> such as decimal point, group separator, digit representation, currency symbol, percent, and permill</li> <li><b>Numeric variations</b> including integers ("123"), fixed-point numbers ("123.4"), scientific notation ("1.23E4"), percentages ("12%"), and currency amounts ("$123")</li> <li><b>Predefined standard patterns</b> that can be used both for parsing and formatting, including {@link #getDecimalFormat() decimal}, {@link #getCurrencyFormat() currency}, {@link #getPercentFormat() percentages}, and {@link #getScientificFormat() scientific}</li> <li><b>Custom patterns</b> and supporting features designed to make it possible to parse and format numbers in any locale, including support for Western, Arabic, and Indic digits</li> </ul>
Patterns
Formatting and parsing are based on customizable patterns that can include a
combination of literal characters and special characters that act as
placeholders and are replaced by their localized counterparts. Many
characters in a pattern are taken literally; they are matched during parsing
and output unchanged during formatting. Special characters, on the other
hand, stand for other characters, strings, or classes of characters. For
example, the '#
' character is replaced by a localized digit.
Often the replacement character is the same as the pattern character. In the
U.S. locale, for example, the ',
' grouping character is
replaced by the same character ',
'. However, the replacement
is still actually happening, and in a different locale, the grouping
character may change to a different character, such as '.
'.
Some special characters affect the behavior of the formatter by their
presence. For example, if the percent character is seen, then the value is
multiplied by 100 before being displayed.
The characters listed below are used in patterns. Localized symbols use the
corresponding characters taken from corresponding locale symbol collection,
which can be found in the properties files residing in the
. To insert
a special character in a pattern as a literal (that is, without any special
meaning) the character must be quoted. There are some exceptions to this
which are noted below.
Symbol | Location | Localized? | Meaning |
---|---|---|---|
0 |
Number | Yes | Digit |
# |
Number | Yes | Digit, zero shows as absent |
. |
Number | Yes | Decimal separator or monetary decimal separator |
- |
Number | Yes | Minus sign |
, |
Number | Yes | Grouping separator |
E |
Number | Yes | Separates mantissa and exponent in scientific notation; need not be quoted in prefix or suffix |
; |
Subpattern boundary | Yes | Separates positive and negative subpatterns |
% |
Prefix or suffix | Yes | Multiply by 100 and show as percentage |
\u2030 (\u005Cu2030) |
Prefix or suffix | Yes | Multiply by 1000 and show as per mille |
\u00A4 (\u005Cu00A4) |
Prefix or suffix | No | Currency sign, replaced by currency symbol; if doubled, replaced by international currency symbol; if present in a pattern, the monetary decimal separator is used instead of the decimal separator |
' |
Prefix or suffix | No | Used to quote special characters in a prefix or suffix; for example,
"'#'#" formats 123 to "#123" ;
to create a single quote itself, use two in succession, such as
"# o''clock" |
pattern | := | subpattern ('; '
subpattern)? |
subpattern | := | prefix? number exponent? suffix? |
number | := | (integer ('. ' fraction)?) |
sigDigits |
prefix | := | '\u005Cu0000 '..'\u005CuFFFD ' -
specialCharacters |
suffix | := | '\u005Cu0000 '..'\u005CuFFFD ' -
specialCharacters |
integer | := | '# '* '0 '*'0 ' |
fraction | := | '0 '* '# '* |
sigDigits | := | '# '* '@ ''@ '* '# '* |
exponent | := | 'E ' '+ '? '0 '* '0 ' |
padSpec | := | '* ' padChar |
padChar | := | '\u005Cu0000 '..'\u005CuFFFD ' - quote |
Notation:
X* | 0 or more instances of X |
X? | 0 or 1 instances of X |
X|Y | either X or Y |
C..D | any character from C up to D, inclusive |
S-T | characters in S, except those in T |
The first subpattern is for positive numbers. The second (optional) subpattern is for negative numbers.
<h3>Example</h3> {@example com.google.gwt.examples.NumberFormatExample}
class NumberFormat { static NumberFormat _cachedDecimalFormat; static NumberFormat getDecimalFormat() { if (_cachedDecimalFormat == null) { _cachedDecimalFormat = new NumberFormat._internal(); } return _cachedDecimalFormat; } NumberFormat._internal(); /** * This method formats a double to produce a string. * * @param number The double to format * @return the formatted number string */ String formatDouble(double number) { if (number.isNaN) { return "NaN"; } return number.toString(); } /** * This method formats a Number to produce a string. * <p> * Any {@link Number} which is not a {@link BigDecimal}, {@link BigInteger}, * or {@link Long} instance is formatted as a {@code double} value. * * @param number The Number instance to format * @return the formatted number string */ String formatInt(int number) { if (number.isNaN) { return "NaN"; } return number.toString(); } /** * This method formats a Number to produce a string. * <p> * Any {@link Number} which is not a {@link BigDecimal}, {@link BigInteger}, * or {@link Long} instance is formatted as a {@code double} value. * * @param number The Number instance to format * @return the formatted number string */ String format(num number) { if (number is int) { return formatInt(number as int); } else { return formatDouble(number as double); } throw new Exception("Unknown type"); } }
Static Methods
NumberFormat getDecimalFormat() #
static NumberFormat getDecimalFormat() { if (_cachedDecimalFormat == null) { _cachedDecimalFormat = new NumberFormat._internal(); } return _cachedDecimalFormat; }
Methods
String format(num number) #
This method formats a Number to produce a string. <p> Any {@link Number} which is not a {@link BigDecimal}, {@link BigInteger}, or {@link Long} instance is formatted as a {@code double} value.
@param number The Number instance to format @return the formatted number string
String format(num number) { if (number is int) { return formatInt(number as int); } else { return formatDouble(number as double); } throw new Exception("Unknown type"); }
String formatDouble(double number) #
This method formats a double to produce a string.
@param number The double to format @return the formatted number string
String formatDouble(double number) { if (number.isNaN) { return "NaN"; } return number.toString(); }
String formatInt(int number) #
This method formats a Number to produce a string. <p> Any {@link Number} which is not a {@link BigDecimal}, {@link BigInteger}, or {@link Long} instance is formatted as a {@code double} value.
@param number The Number instance to format @return the formatted number string
String formatInt(int number) { if (number.isNaN) { return "NaN"; } return number.toString(); }