ICU Enhancement
This class is ICU's replacement for java..
ICU
Methods, fields, and other functionality specific to ICU are labeled 'ICU'.
Important Note
New users are strongly encouraged to see if
NumberFormatter fits their use case. Although not deprecated, this
class, NumberFormat, is only provided for java.text.NumberFormat compatibility.
NumberFormat is the abstract base class for all number
formats. This class provides the interface for formatting and parsing
numbers. NumberFormat also provides methods for determining
which locales have number formats, and what their names are.
NumberFormat helps you to format and parse numbers for any locale.
Your code can be completely independent of the locale conventions for
decimal points, thousands-separators, or even the particular decimal
digits used, or whether the number format is even decimal.
To format a number for the current Locale, use one of the factory class methods:
If you are formatting multiple numbers, it is more efficient to get the format and use it multiple times so that the system doesn't have to fetch the information about the local language and country conventions multiple times.myString = NumberFormat.getInstance().format(myNumber);
NumberFormat nf = NumberFormat.getInstance();
for (int i = 0; i < a.length; ++i) {
output.println(nf.format(myNumber[i]) + "; ");
}
To format a number for a different Locale, specify it in the
call to getInstance.
You can also use aNumberFormat nf = NumberFormat.getInstance(Locale.FRENCH);
NumberFormat to parse numbers:
UsemyNumber = nf.parse(myString);
getInstance or getNumberInstance to get the
normal number format. Use getIntegerInstance to get an
integer number format. Use getCurrencyInstance to get the
currency number format. And use getPercentInstance to get a
format for displaying percentages. Some factory methods are found within
subclasses of NumberFormat. With this format, a fraction like
0.53 is displayed as 53%.
Starting from ICU 4.2, you can use getInstance() by passing in a 'style' as parameter to get the correct instance. For example, use getInstance(...NUMBERSTYLE) to get the normal number format, getInstance(...PERCENTSTYLE) to get a format for displaying percentage, getInstance(...SCIENTIFICSTYLE) to get a format for displaying scientific number, getInstance(...INTEGERSTYLE) to get an integer number format, getInstance(...CURRENCYSTYLE) to get the currency number format, in which the currency is represented by its symbol, for example, "$3.00". getInstance(...ISOCURRENCYSTYLE) to get the currency number format, in which the currency is represented by its ISO code, for example "USD3.00". getInstance(...PLURALCURRENCYSTYLE) to get the currency number format, in which the currency is represented by its full name in plural format, for example, "3.00 US dollars" or "1.00 US dollar".
You can also control the display of numbers with such methods as
setMinimumFractionDigits.
If you want even more control over the format or parsing,
or want to give your users more control,
you can try casting the NumberFormat you get from the factory methods
to a DecimalFormat. This will work for the vast majority
of locales; just remember to put it in a try block in case you
encounter an unusual one.
NumberFormat is designed such that some controls work for formatting and others work for parsing. The following is the detailed description for each these control methods,
setParseIntegerOnly : only affects parsing, e.g. if true, "3456.78" -> 3456 (and leaves the parse position just after '6') if false, "3456.78" -> 3456.78 (and leaves the parse position just after '8') This is independent of formatting. If you want to not show a decimal point where there might be no digits after the decimal point, use setDecimalSeparatorAlwaysShown on DecimalFormat.
You can also use forms of the parse and format
methods with ParsePosition and FieldPosition to
allow you to:
FieldPosition in your format call, with
field = INTEGER_FIELD. On output,
getEndIndex will be set to the offset between the
last character of the integer and the decimal. Add
(desiredSpaceCount - getEndIndex) spaces at the front of the string.
getEndIndex.
Then move the pen by
(desiredPixelWidth - widthToAlignmentPoint) before drawing the text.
It also works where there is no decimal, but possibly additional
characters at the end, e.g., with parentheses in negative
numbers: "(12)" for -12.
Number formats are generally not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.
DecimalFormat is the concrete implementation of NumberFormat, and the NumberFormat API is essentially an abstraction from DecimalFormat's API. Refer to DecimalFormat for more information about this API.
see DecimalFormat see java.text.ChoiceFormat| Modifier and Type | Class and Description |
|---|---|
| public static class | NumberFormat.Field
The instances of this inner class are used as attribute keys and values in AttributedCharacterIterator that NumberFormat.formatToCharacterIterator() method returns. |
| public abstract static class | NumberFormat.NumberFormatFactory
A NumberFormatFactory is used to register new number formats. |
| pack-priv abstract static class | |
| public abstract static class | NumberFormat.SimpleNumberFormatFactory
A NumberFormatFactory that supports a single locale. |
| Modifier and Type | Field and Description |
|---|---|
| public static final int | |
| private DisplayContext | |
| public static final int | |
| private Currency | currency
Currency object used to format currencies. |
| public static final int | |
| pack-priv static final int | |
| private static final char[] | |
| private static final String | |
| public static final int | FRACTION_FIELD
Field constant used to construct a FieldPosition object. |
| private boolean | groupingUsed
True if the the grouping (i.e. thousands) separator is used when formatting and parsing numbers. |
| public static final int | INTEGER_FIELD
Field constant used to construct a FieldPosition object. |
| public static final int | INTEGERSTYLE
ICU |
| public static final int | |
| private byte | maxFractionDigits
The maximum number of digits allowed in the fractional portion of a number. |
| private int | maximumFractionDigits
The maximum number of digits allowed in the fractional portion of a number. |
| private int | maximumIntegerDigits
The maximum number of digits allowed in the integer portion of a number. |
| private byte | maxIntegerDigits
The maximum number of digits allowed in the integer portion of a number. |
| private byte | minFractionDigits
The minimum number of digits allowed in the fractional portion of a number. |
| private int | minimumFractionDigits
The minimum number of digits allowed in the fractional portion of a number. |
| private int | minimumIntegerDigits
The minimum number of digits allowed in the integer portion of a number. |
| private byte | minIntegerDigits
The minimum number of digits allowed in the integer portion of a number. |
| public static final int | NUMBERSTYLE
ICU |
| private boolean | parseIntegerOnly
True if this format will parse numbers as integers only. |
| private boolean | |
| public static final int | PERCENTSTYLE
ICU |
| public static final int | |
| public static final int | |
| private int | serialVersionOnStream
Describes the version of |
| private static final long | |
| private static NumberFormat. | |
| public static final int |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public Object | |
| pack-priv static NumberFormat | |
| public boolean | Returns: true if the object is equal to this.the object to compare against obj)Overrides java. |
| public StringBuffer | format(Object
The object to format number, StringBuffer where the text is to be appended toAppendTo, FieldPosition A pos)FieldPosition identifying a field
in the formatted textImplements abstract java. |
| public final String | |
| public final String | |
| public final String | |
| public final String | |
| public final String | |
| public final String | |
| public abstract StringBuffer | |
| public abstract StringBuffer | |
| public abstract StringBuffer | |
| public abstract StringBuffer | |
| public abstract StringBuffer | |
| public StringBuffer | |
| public static Locale[] | Returns: the available localesReturns the list of Locales for which NumberFormats are available. |
| public static ULocale[] | |
| public DisplayContext | Returns: the current DisplayContext setting for the specified typethe DisplayContext.Type whose value to return typeICU |
| public Currency | |
| public static final NumberFormat | Returns: a number format for currencyNote New users are strongly encouraged to use
|
| public static NumberFormat | Returns: a number format for currencyNote New users are strongly encouraged to use
|
| public static NumberFormat | |
| protected Currency | Returns: a non-null Currency
Deprecated
This API is ICU internal only.
Returns the currency in effect for this formatter.
|
| public static final NumberFormat | |
| public static NumberFormat | getInstance(Locale inLocale)
Note New users are strongly encouraged to use
|
| public static NumberFormat | |
| public static final NumberFormat | |
| public static NumberFormat | |
| public static NumberFormat | getInstance(ULocale
the specific locale. desiredLocale, int number format style choice)Note New users are strongly encouraged to use
|
| public static final NumberFormat | Returns: a number format for integer valuesNote New users are strongly encouraged to use
|
| public static NumberFormat | Returns: a number format for integer valuesthe locale for which a number format is needed inLocale)Note New users are strongly encouraged to use
|
| public static NumberFormat | Returns: a number format for integer valuesthe locale for which a number format is needed inLocale)
ICU |
| public int | Returns: the maximum number of fraction digitsReturns the maximum number of digits allowed in the fraction portion of a number. |
| public int | Returns: the maximum number of integer digitsReturns the maximum number of digits allowed in the integer portion of a number. |
| public int | Returns: the minimum number of fraction digitsReturns the minimum number of digits allowed in the fraction portion of a number. |
| public int | Returns: the minimum number of integer digitsReturns the minimum number of digits allowed in the integer portion of a number. |
| public static final NumberFormat | getNumberInstance()
Note New users are strongly encouraged to use
|
| public static NumberFormat | getNumberInstance(Locale inLocale)
Note New users are strongly encouraged to use
|
| public static NumberFormat | |
| protected static String | Returns: the patternthe locale of the data. forLocale, int the pattern format. choice)
Deprecated
ICU 3.4 subclassers should override getPattern(ULocale, int) instead of this method.
Returns the pattern for the provided locale and choice.
|
| protected static String | Returns: the patternthe locale of the data. forLocale, int the pattern format. choice)Returns the pattern for the provided locale and choice. |
| public static String | Returns: the patternthe locale of the data. forLocale, int the pattern format. choice)
Deprecated
This API is ICU internal only.
Returns the pattern for the provided locale and choice.
|
| public static String | Returns: the patternthe locale of the data. forLocale, String The name of the numbering system, like "latn". nsName, int the pattern format. choice)
Deprecated
This API is ICU internal only.
Returns the pattern for the provided locale, numbering system, and choice.
|
| public static final NumberFormat | Returns: a number format for percentsNote New users are strongly encouraged to use
|
| public static NumberFormat | Returns: a number format for percentsNote New users are strongly encouraged to use
|
| public static NumberFormat | |
| public int | Returns: A rounding mode, betweenBigDecimal.ROUND_UP
and BigDecimal.ROUND_UNNECESSARY.Returns the rounding mode used in this NumberFormat. |
| public static final NumberFormat | |
| public static NumberFormat | |
| public static NumberFormat | |
| private static NumberFormat. | |
| public int | |
| public boolean | |
| public boolean | Returns: true if this will parse integers onlyReturns true if this format will parse numbers as integers only. |
| public boolean | |
| public abstract Number | parse(String text, ParsePosition parsePosition)
Returns a Long if possible (e.g., within the range [Long.MIN_VALUE, Long.MAX_VALUE] and with no decimals); otherwise, returns another type, such as a BigDecimal, BigInteger, or Double. |
| public Number | |
| public CurrencyAmount | Returns: a CurrencyAmount, or null upon failurethe text to parse text, ParsePosition input-output position; on input, the position within
text to match; must have 0 <= pos.getIndex() < text.length();
on output, the position after the last matched character. If
the parse fails, the position in unchanged upon output. pos)Parses text from the given string as a CurrencyAmount. |
| public final Object | Returns: the parsed number, or nullthe String to parse source, ParsePosition the position at which to start the parse parsePosition)Implements abstract java. |
| private void | |
| public static Object | Returns: a key with which to unregister the factorythe factory to register factoryICU |
| public void | |
| public void | setCurrency(Currency
new currency object to use. May be null for
some subclasses. theCurrency)Sets the Currency object used to display currency amounts. |
| public void | setGroupingUsed(boolean
true to use grouping. newValue)Sets whether or not grouping will be used in this format. |
| public void | setMaximumFractionDigits(int
the maximum number of fraction digits to be shown; if
less than zero, then zero is used. The concrete subclass may enforce an
upper limit to this value appropriate to the numeric type being formatted. newValue)Sets the maximum number of digits allowed in the fraction portion of a number. |
| public void | setMaximumIntegerDigits(int
the maximum number of integer digits to be shown; if
less than zero, then zero is used. Subclasses might enforce an
upper limit to this value appropriate to the numeric type being formatted. newValue)Sets the maximum number of digits allowed in the integer portion of a number. |
| public void | setMinimumFractionDigits(int
the minimum number of fraction digits to be shown; if
less than zero, then zero is used. Subclasses might enforce an
upper limit to this value appropriate to the numeric type being formatted. newValue)Sets the minimum number of digits allowed in the fraction portion of a number. |
| public void | setMinimumIntegerDigits(int
the minimum number of integer digits to be shown; if
less than zero, then zero is used. Subclasses might enforce an
upper limit to this value appropriate to the numeric type being formatted. newValue)Sets the minimum number of digits allowed in the integer portion of a number. |
| public void | setParseIntegerOnly(boolean
true if this should parse integers only value)Sets whether to ignore the fraction part of a number when parsing (defaults to false). |
| public void | |
| public void | setRoundingMode(int
A rounding mode, between
roundingMode)BigDecimal.ROUND_UP and
BigDecimal.ROUND_UNNECESSARY.Set the rounding mode used in this NumberFormat. |
| public static boolean | Returns: true if the object was successfully unregistereda key obtained from registerFactory registryKey)ICU |
| private void | writeObject(ObjectOutputStream stream)
Write out the default serializable data, after first setting
the |