All Packages Class Hierarchy This Package Previous Next Index
Class java.text.NumberFormat
java.lang.Object
|
+----java.text.Format
|
+----java.text.NumberFormat
- public class NumberFormat
- extends Format
- implements Cloneable
Abstract base class for all number formats.
Provides interface for formatting and parsing a number.
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
static factory methods:
myString = NumberFormat.getDefault().format(myNumber);
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.
NumberFormat nf = NumberFormat.getDefault();
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 getDefault().
NumberFormat nf = NumberFormat.getDefault(Locale.FRENCH);
You can use a NumberFormat to parse also.
myNumber = nf.parse(myString);
Use getDefault to get the normal number format for that country.
There are other static factory methods available.
Use getCurrency to get the currency number format for that country.
Use getPercent to get a format for displaying percentages. With this
format, a fraction from 0.53 is displayed as 53%.
You can also control the display of numbers with such methods as
setMinimumDecimalCount.
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 DecimalNumberFormat. This will work for the vast majority
of countries; just remember to put it in a try block in case you
encounter an unusual one.
You can also use forms of the parse and format methods with
ParseStatus and FormatStatus to
allow you to
- progressively parse through pieces of a string.
- align the decimal point and other areas.
For example, you can align numbers in two ways.
If you are using a monospaced font with spacing for alignment,
you can pass the FormatStatus in your format call, with
alignField = INTEGER_FIELD. On output, alignEnd will be set to the
offset between the last character of the integer and the decimal. Add
(desiredSpaceCount - alignEnd) spaces at the front of the string.
If you are using proportional fonts,
instead of padding with spaces, measure the width
of the string in pixels from the start to alignEnd.
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.
- See Also:
- DecimalFormat, ChoiceFormat
-
DECIMAL_FIELD
- Decimal field is the alignment field.
-
DENOMINATOR_FIELD
- Denominator field is the alignment field.
-
EXPONENT_FIELD
- Exponent field is the alignment field.
-
INTEGER_FIELD
- Integer field is the alignment field.
-
NUMERATOR_FIELD
- Numerator field is the alignment field.
-
NumberFormat()
-
-
clone()
- Overrides Cloneable
-
format(double)
- Specialization of format.
-
format(double, StringBuffer, FormatStatus)
- Specialization of format.
-
format(long)
- Specialization of format.
-
format(long, StringBuffer, FormatStatus)
- Specialization of format.
-
format(Object, StringBuffer, FormatStatus)
- Formats an object to produce a string.
-
getAvailableLocales()
- Get the set of Locales for which NumberFormats are installed
-
getCurrencySymbol()
- Get the currency symbol of the current locale.
-
getCurrencySymbol(Locale)
- Get the currency symbol of the desired locale.
-
getDefault()
- Gets a general-purpose number format for the current default locale
-
getDefault(Locale)
- Gets a general-purpose number format for the specified locale
-
getDefaultCurrency()
- Gets a currency format for the current default locale
-
getDefaultCurrency(Locale)
- Gets a currency format for the specified locale
-
getDefaultPercent()
- Gets a percentage format for the current default locale
-
getDefaultPercent(Locale)
- Gets a percentage format for the specified locale
-
getDisplayName(Locale)
- Get name of the object for the desired Locale, in the langauge of the
default locale.
-
getDisplayName(Locale, Locale)
- Get name of the object for the desired Locale, in the desired langauge.
-
getIntlCurrencySymbol()
- Get the international currency symbol of the current locale.
-
getIntlCurrencySymbol(Locale)
- Get the international currency symbol of the desired locale.
-
getMaximumDecimalCount()
- Allows you to get the maximum and minimum digit counts for the
decimal portion.
-
getMaximumIntegerCount()
- Allows you to get the maximum digit count for the integer portion.
-
getMinimumDecimalCount()
- Allows you to get the maximum and minimum digit counts for the
decimal portion.
-
getMinimumIntegerCount()
- Allows you to get the minimum digit count for the integer portion.
-
isDecimalUsedWithInteger()
- Allows you to get the behavior of the decimal separator with integers.
-
isIntegerOnly()
- Gets flag that indicates that it stops at a decimal point
(or equivalent; e.g.
-
isThousandsUsed()
- Allows you to get the behavior of the thousands separator.
-
parse(String)
- Convenience method.
-
parse(String, ParseStatus)
- Returns a Long if possible (e.g.
-
parseObject(String, ParseStatus)
- Parses a string to produce an object.
-
setDecimalUsedWithInteger(boolean)
- Allows you to set the behavior of the decimal separator with integers.
-
setIntegerOnly(boolean)
- Sets flag that indicates that it stops at a decimal point
(or equivalent; e.g.
-
setMaximumDecimalCount(int)
- Allows you to set the maximum and minimum digit counts for the
decimal portion.
-
setMaximumIntegerCount(int)
- Allows you to set the maximum digit count for the integer portion.
-
setMinimumDecimalCount(int)
- Allows you to set the maximum and minimum digit counts for the
decimal portion.
-
setMinimumIntegerCount(int)
- Allows you to set the minimum digit count for the integer portion.
-
setThousandsUsed(boolean)
- Allows you to set the behavior of the thousands separator.
INTEGER_FIELD
public final static byte INTEGER_FIELD
- Integer field is the alignment field.
DECIMAL_FIELD
public final static byte DECIMAL_FIELD
- Decimal field is the alignment field.
NUMERATOR_FIELD
public final static byte NUMERATOR_FIELD
- Numerator field is the alignment field.
DENOMINATOR_FIELD
public final static byte DENOMINATOR_FIELD
- Denominator field is the alignment field.
EXPONENT_FIELD
public final static byte EXPONENT_FIELD
- Exponent field is the alignment field.
NumberFormat
public NumberFormat()
format
public final StringBuffer format(Object number,
StringBuffer toAppendTo,
FormatStatus status)
- Formats an object to produce a string.
- Overrides:
- format in class Format
parseObject
public final Object parseObject(String source,
ParseStatus status)
- Parses a string to produce an object.
- Overrides:
- parseObject in class Format
format
public final String format(double number)
- Specialization of format.
- See Also:
- format
format
public final String format(long number)
- Specialization of format.
- See Also:
- format
format
public abstract StringBuffer format(double number,
StringBuffer toAppendTo,
FormatStatus status)
- Specialization of format.
- See Also:
- format
format
public abstract StringBuffer format(long number,
StringBuffer toAppendTo,
FormatStatus status)
- Specialization of format.
- See Also:
- format
parse
public abstract Number parse(String text,
ParseStatus status)
- Returns a Long if possible (e.g. within range [Long.MIN_VALUE,
Long.MAX_VALUE], and with no decimals), otherwise a Double.
If IntegerOnly is set, will stop at a decimal
point (or equivalent; e.g. for rational numbers "1 2/3", will stop
after the 1).
Does not throw an exception; if no object can be parsed, startAt is
unchanged!
- See Also:
- isIntegerOnly, parseObject
parse
public Number parse(String text) throws FormatException
- Convenience method.
- See Also:
- format
isIntegerOnly
public boolean isIntegerOnly()
- Gets flag that indicates that it stops at a decimal point
(or equivalent; e.g. for rational numbers "1 2/3", will stop
after the 1 if integerOnly; continue to parse the 2/3 if !integerOnly).
setIntegerOnly
public void setIntegerOnly(boolean value)
- Sets flag that indicates that it stops at a decimal point
(or equivalent; e.g. for rational numbers "1 2/3", will stop
after the 1 if integerOnly; continue to parse the 2/3 if !integerOnly).
getDefault
public final static synchronized NumberFormat getDefault()
- Gets a general-purpose number format for the current default locale
- Returns:
- general-purpose number format
getDefaultCurrency
public final static synchronized NumberFormat getDefaultCurrency()
- Gets a currency format for the current default locale
- Returns:
- currency format
getDefaultPercent
public final static synchronized NumberFormat getDefaultPercent()
- Gets a percentage format for the current default locale
- Returns:
- percentage number format
getDefault
public static synchronized NumberFormat getDefault(Locale inLocale)
- Gets a general-purpose number format for the specified locale
- Returns:
- general-purpose number format
getDefaultCurrency
public static synchronized NumberFormat getDefaultCurrency(Locale inLocale)
- Gets a currency format for the specified locale
- Returns:
- currency format
getDefaultPercent
public static synchronized NumberFormat getDefaultPercent(Locale inLocale)
- Gets a percentage format for the specified locale
- Returns:
- percentage number format
getAvailableLocales
public static synchronized Locale[] getAvailableLocales()
- Get the set of Locales for which NumberFormats are installed
- Returns:
- available locales
getDisplayName
public static synchronized String getDisplayName(Locale objectLocale,
Locale displayLocale)
- Get name of the object for the desired Locale, in the desired langauge.
- Parameters:
- objectLocale - must be from getAvailableLocales.
- displayLocale - specifies the desired locale for output.
Uses best match.
- Returns:
- user-displayable name
- Throws: throws
- MissingResourceException if objectLocale is
not from must be from getAvailableLocales
- See Also:
- ResourceBundle
getDisplayName
public final static String getDisplayName(Locale objectLocale)
- Get name of the object for the desired Locale, in the langauge of the
default locale.
- Parameters:
- objectLocale - must be from getAvailableLocales.
- Returns:
- user-displayable name
- Throws: throws
- MissingResourceException if objectLocale is
not from must be from getAvailableLocales
- See Also:
- ResourceBundle
getCurrencySymbol
public final String getCurrencySymbol()
- Get the currency symbol of the current locale.
- Returns:
- the currency symbol
getCurrencySymbol
public final String getCurrencySymbol(Locale loc)
- Get the currency symbol of the desired locale.
- Parameters:
- loc - the desired locale
- Returns:
- the currency symbol
getIntlCurrencySymbol
public final String getIntlCurrencySymbol()
- Get the international currency symbol of the current locale.
- Returns:
- the international currency symbol
getIntlCurrencySymbol
public final String getIntlCurrencySymbol(Locale loc)
- Get the international currency symbol of the desired locale.
- Parameters:
- loc - the desired locale
- Returns:
- the international currency symbol
clone
public Object clone()
- Overrides Cloneable
- Overrides:
- clone in class Object
isThousandsUsed
public boolean isThousandsUsed()
- Allows you to get the behavior of the thousands separator.
Thousands ON is ignored if there are fewer than thousandsInterval
integer digits.
Example: Thousands ON: 12345 -> 12,345; OFF: 12345 -> 12345
setThousandsUsed
public void setThousandsUsed(boolean newValue)
- Allows you to set the behavior of the thousands separator.
Thousands ON is ignored if there are fewer than thousandsInterval
integer digits.
Example: Thousands ON: 12345 -> 12,345; OFF: 12345 -> 12345
isDecimalUsedWithInteger
public boolean isDecimalUsedWithInteger()
- Allows you to get the behavior of the decimal separator with integers.
(The decimal separator will always appear with decimals.)
Example: Decimal ON: 12345 -> 12345.; OFF: 12345 -> 12345
setDecimalUsedWithInteger
public void setDecimalUsedWithInteger(boolean newValue)
- Allows you to set the behavior of the decimal separator with integers.
(The decimal separator will always appear with decimals.)
Example: Decimal ON: 12345 -> 12345.; OFF: 12345 -> 12345
getMaximumIntegerCount
public int getMaximumIntegerCount()
- Allows you to get the maximum digit count for the integer portion.
Example: with different values, 123 -> 123; 00,123;
setMaximumIntegerCount
public void setMaximumIntegerCount(int newValue)
- Allows you to set the maximum digit count for the integer portion.
Example: with different values, 123 -> 123; 00,123;
getMinimumIntegerCount
public int getMinimumIntegerCount()
- Allows you to get the minimum digit count for the integer portion.
Example: with different values, 123 -> 123; 00,123;
setMinimumIntegerCount
public void setMinimumIntegerCount(int newValue)
- Allows you to set the minimum digit count for the integer portion.
Example: with different values, 123 -> 123; 00,123;
getMaximumDecimalCount
public int getMaximumDecimalCount()
- Allows you to get the maximum and minimum digit counts for the
decimal portion.
Example: with different values, 1.246 -> 1.25, 1.246000
setMaximumDecimalCount
public void setMaximumDecimalCount(int newValue)
- Allows you to set the maximum and minimum digit counts for the
decimal portion.
Example: with different values, 1.246 -> 1.25, 1.246000
getMinimumDecimalCount
public int getMinimumDecimalCount()
- Allows you to get the maximum and minimum digit counts for the
decimal portion.
Example: with different values, 1.246 -> 1.25, 1.246000
setMinimumDecimalCount
public void setMinimumDecimalCount(int newValue)
- Allows you to set the maximum and minimum digit counts for the
decimal portion.
Example: with different values, 1.246 -> 1.25, 1.246000
All Packages Class Hierarchy This Package Previous Next Index