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

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

Variable Index

 o DECIMAL_FIELD
Decimal field is the alignment field.
 o DENOMINATOR_FIELD
Denominator field is the alignment field.
 o EXPONENT_FIELD
Exponent field is the alignment field.
 o INTEGER_FIELD
Integer field is the alignment field.
 o NUMERATOR_FIELD
Numerator field is the alignment field.

Constructor Index

 o NumberFormat()

Method Index

 o clone()
Overrides Cloneable
 o format(double)
Specialization of format.
 o format(double, StringBuffer, FormatStatus)
Specialization of format.
 o format(long)
Specialization of format.
 o format(long, StringBuffer, FormatStatus)
Specialization of format.
 o format(Object, StringBuffer, FormatStatus)
Formats an object to produce a string.
 o getAvailableLocales()
Get the set of Locales for which NumberFormats are installed
 o getCurrencySymbol()
Get the currency symbol of the current locale.
 o getCurrencySymbol(Locale)
Get the currency symbol of the desired locale.
 o getDefault()
Gets a general-purpose number format for the current default locale
 o getDefault(Locale)
Gets a general-purpose number format for the specified locale
 o getDefaultCurrency()
Gets a currency format for the current default locale
 o getDefaultCurrency(Locale)
Gets a currency format for the specified locale
 o getDefaultPercent()
Gets a percentage format for the current default locale
 o getDefaultPercent(Locale)
Gets a percentage format for the specified locale
 o getDisplayName(Locale)
Get name of the object for the desired Locale, in the langauge of the default locale.
 o getDisplayName(Locale, Locale)
Get name of the object for the desired Locale, in the desired langauge.
 o getIntlCurrencySymbol()
Get the international currency symbol of the current locale.
 o getIntlCurrencySymbol(Locale)
Get the international currency symbol of the desired locale.
 o getMaximumDecimalCount()
Allows you to get the maximum and minimum digit counts for the decimal portion.
 o getMaximumIntegerCount()
Allows you to get the maximum digit count for the integer portion.
 o getMinimumDecimalCount()
Allows you to get the maximum and minimum digit counts for the decimal portion.
 o getMinimumIntegerCount()
Allows you to get the minimum digit count for the integer portion.
 o isDecimalUsedWithInteger()
Allows you to get the behavior of the decimal separator with integers.
 o isIntegerOnly()
Gets flag that indicates that it stops at a decimal point (or equivalent; e.g.
 o isThousandsUsed()
Allows you to get the behavior of the thousands separator.
 o parse(String)
Convenience method.
 o parse(String, ParseStatus)
Returns a Long if possible (e.g.
 o parseObject(String, ParseStatus)
Parses a string to produce an object.
 o setDecimalUsedWithInteger(boolean)
Allows you to set the behavior of the decimal separator with integers.
 o setIntegerOnly(boolean)
Sets flag that indicates that it stops at a decimal point (or equivalent; e.g.
 o setMaximumDecimalCount(int)
Allows you to set the maximum and minimum digit counts for the decimal portion.
 o setMaximumIntegerCount(int)
Allows you to set the maximum digit count for the integer portion.
 o setMinimumDecimalCount(int)
Allows you to set the maximum and minimum digit counts for the decimal portion.
 o setMinimumIntegerCount(int)
Allows you to set the minimum digit count for the integer portion.
 o setThousandsUsed(boolean)
Allows you to set the behavior of the thousands separator.

Variables

 o INTEGER_FIELD
  public final static byte INTEGER_FIELD
Integer field is the alignment field.

 o DECIMAL_FIELD
  public final static byte DECIMAL_FIELD
Decimal field is the alignment field.

 o NUMERATOR_FIELD
  public final static byte NUMERATOR_FIELD
Numerator field is the alignment field.

 o DENOMINATOR_FIELD
  public final static byte DENOMINATOR_FIELD
Denominator field is the alignment field.

 o EXPONENT_FIELD
  public final static byte EXPONENT_FIELD
Exponent field is the alignment field.

Constructors

 o NumberFormat
  public NumberFormat()

Methods

 o format
  public final StringBuffer format(Object number,
                                   StringBuffer toAppendTo,
                                   FormatStatus status)
Formats an object to produce a string.

Overrides:
format in class Format
 o parseObject
  public final Object parseObject(String source,
                                  ParseStatus status)
Parses a string to produce an object.

Overrides:
parseObject in class Format
 o format
  public final String format(double number)
Specialization of format.

See Also:
format
 o format
  public final String format(long number)
Specialization of format.

See Also:
format
 o format
  public abstract StringBuffer format(double number,
                                      StringBuffer toAppendTo,
                                      FormatStatus status)
Specialization of format.

See Also:
format
 o format
  public abstract StringBuffer format(long number,
                                      StringBuffer toAppendTo,
                                      FormatStatus status)
Specialization of format.

See Also:
format
 o 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
 o parse
  public Number parse(String text) throws FormatException
Convenience method.

See Also:
format
 o 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).

 o 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).

 o getDefault
  public final static synchronized NumberFormat getDefault()
Gets a general-purpose number format for the current default locale

Returns:
general-purpose number format
 o getDefaultCurrency
  public final static synchronized NumberFormat getDefaultCurrency()
Gets a currency format for the current default locale

Returns:
currency format
 o getDefaultPercent
  public final static synchronized NumberFormat getDefaultPercent()
Gets a percentage format for the current default locale

Returns:
percentage number format
 o getDefault
  public static synchronized NumberFormat getDefault(Locale inLocale)
Gets a general-purpose number format for the specified locale

Returns:
general-purpose number format
 o getDefaultCurrency
  public static synchronized NumberFormat getDefaultCurrency(Locale inLocale)
Gets a currency format for the specified locale

Returns:
currency format
 o getDefaultPercent
  public static synchronized NumberFormat getDefaultPercent(Locale inLocale)
Gets a percentage format for the specified locale

Returns:
percentage number format
 o getAvailableLocales
  public static synchronized Locale[] getAvailableLocales()
Get the set of Locales for which NumberFormats are installed

Returns:
available locales
 o 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
 o 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
 o getCurrencySymbol
  public final String getCurrencySymbol()
Get the currency symbol of the current locale.

Returns:
the currency symbol
 o getCurrencySymbol
  public final String getCurrencySymbol(Locale loc)
Get the currency symbol of the desired locale.

Parameters:
loc - the desired locale
Returns:
the currency symbol
 o getIntlCurrencySymbol
  public final String getIntlCurrencySymbol()
Get the international currency symbol of the current locale.

Returns:
the international currency symbol
 o 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
 o clone
  public Object clone()
Overrides Cloneable

Overrides:
clone in class Object
 o 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

 o 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

 o 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

 o 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

 o getMaximumIntegerCount
  public int getMaximumIntegerCount()
Allows you to get the maximum digit count for the integer portion.

Example: with different values, 123 -> 123; 00,123;

 o 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;

 o getMinimumIntegerCount
  public int getMinimumIntegerCount()
Allows you to get the minimum digit count for the integer portion.

Example: with different values, 123 -> 123; 00,123;

 o 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;

 o 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

 o 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

 o 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

 o 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