Abstract Class NumberFormat in Java

digitization, transformation, binary

Abstract Class NumberFormat in Java

The NumberFormat class formats and parses numbers in a locale-specific way. As an abstract class, it cannot be instantiated directly, but it provides a number of static methods that return instances of a concrete subclass that we can use for formatting.

The getInstance() method returns a NumberFormat object suitable for normal formatting of numbers in either the default locale or in a specified locale.

Ads code goes here

The NumberFormat class formats and parses numbers in a locale-specific manner. NumberFormat is an abstract class, but it provides factory methods that return useful instances of NumberFormat subclasses. These factory methods come in three groups:

  • The getCurrencyInstance() methods return objects that format and parse currency values.
  • The getNumberInstance() methods return objects that format and parse normal numbers.
  • The getPercentInstance() methods return objects that format percentage values.

getCurrencyInstance() and getPercentInstance() return NumberFormat objects for formatting numbers that represent monetary amounts and percentages in either the default locale or in a specified locale.

getAvailableLocales() returns an array of locales for which NumberFormat objects are available. Once we have created a suitable NumberFormat object, we can customize its locale-independent behavior with setMaximumFractionDigits(), setGroupingUsed() and similar set methods.

In order to customize the locale-dependent behavior, we can use instanceof to test if the NumberFormat object is an instance of DecimalFormat, and if so, we can cast it to that type.

The DecimalFormat class provides complete control over number formatting. Note, however, that a NumberFormat customized in this way may no longer be appropriate for the desired
locale.

After creating and customizing a NumberFormat object, we can use the various format() methods to convert numbers to strings or string buffers, and we can use the parse() or parseObject() methods to convert strings to numbers.

The constants defined by this class are intended to be used by the FieldPosition object.

The NumberFormat class is not intended for the display of very large or very small numbers that require exponential notation, and it may not gracefully handle infinite or NaN (not-a-number) values.

For example, to format a number as an Italian currency value, we can use this code:

code>double salary = 1234.56;
System.out.println(NumberFormat.getCurrencyInstance(Locale.ITALY).format(salary));

This produces the following output:
L 1.234,56

The NumberFormat class defines two field constants that represent the integer and fractional fields in a formatted number. These field constants create FieldPosition objects.

The structure of Abstract Class NumberFormat in Java is given as

public abstract class java.text.NumberFormat extends java.text.Format implements java.lang.Cloneable {
// Default Constructor: 
public NumberFormat();
// Constants
public static final int FRACTION_FIELD;
public static final int INTEGER_FIELD;
// Class Methods
public static Locale[] getAvailableLocales();
public static final NumberFormat getCurrencyInstance();
public static NumberFormat getCurrencyInstance(Locale inLocale);
public static final NumberFormat getInstance();
public static NumberFormat getInstance(Locale inLocale);
public static final NumberFormat getNumberInstance();
public static NumberFormat getNumberInstance(Locale inLocale);
public static final NumberFormat getPercentInstance();
public static NumberFormat getPercentInstance(Locale inLocale);
// Public Instance Methods
public Object clone(); // Overrides Format
public boolean equals(Object obj); // Overrides Object
public final StringBuffer format(Object number, StringBuffer toAppendTo,FieldPosition pos); // Defines Format
public final String format(double number);
public final String format(long number);
public abstract StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos);
public abstract StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos);
public int getMaximumFractionDigits();
public int getMaximumIntegerDigits();
public int getMinimumFractionDigits();
public int getMinimumIntegerDigits();
public int hashCode(); // Overrides Object
public boolean isGroupingUsed();
public boolean isParseIntegerOnly();
public abstract Number parse(String text, ParsePosition parsePosition);
public Number parse(String text) throws ParseException;
public final Object parseObject(String source, ParsePosition parsePosition); // Defines Format
public void setGroupingUsed(boolean newValue);
public void setMaximumFractionDigits(int newValue);
public void setMaximumIntegerDigits(int newValue);
public void setMinimumFractionDigits(int newValue);
public void setMinimumIntegerDigits(int newValue);
public void setParseIntegerOnly(boolean value);
}

The details of the class structure are given as follows:

public static final int FRACTION_FIELD;

public static final int FRACTION_FIELD represents a field constant that represents the fractional part of the number.

public static final int INTEGER_FIELD;

public static final int INTEGER_FIELD represents a field constant that represents the integer part of the number.

public static Locale[] getAvailableLocales();

public static Locale[] getAvailableLocales() method returns an array of the Locale objects for which this class can create NumberFormat objects.

READ  Abstract Class DatagramSocketImpl in Java

This method returns an array of Locale objects.

public static final NumberFormat getCurrencyInstance();

public static final NumberFormat getCurrencyInstance() method creates a NumberFormat that formats and parses currency values in the default locale.

This method returns a NumberFormat appropriate for the default Locale that formats currency values.

public static NumberFormat getCurrencyInstance(Locale inLocale);

public static NumberFormat getCurrencyInstance(Locale inLocale) method creates a NumberFormat that formats and parses currency values in the given locale.

This method returns a NumberFormat appropriate for the given Locale that formats currency values.

Parameter
inLocale – The Locale to use.

public static final NumberFormat getInstance();

public static final NumberFormat getInstance() method creates a default NumberFormat that formats and parses values in the default locale.

This method returns a default NumberFormat appropriate for the default Locale.

public static NumberFormat getInstance(Locale inLocale);

public static NumberFormat getInstance(Locale inLocale) method creates a NumberFormat that formats and parses values in the given locale.

This method returns a default NumberFormat appropriate for the given Locale.

Parameter
inLocale – The Locale to use.

public static final NumberFormat getNumberInstance();

public static final NumberFormat getNumberInstance() method creates a NumberFormat that formats and parses number values in the default locale.

This method returns a NumberFormat appropriate for the default Locale that formats normal numbers.

public static NumberFormat getNumberInstance(Locale inLocale);

public static NumberFormat getNumberInstance(Locale inLocale) method creates a NumberFormat that formats and parses number values in the given locale.

This method returns a NumberFormat appropriate for the given Locale that formats normal numbers.

Parameter
inLocale – The Locale to use.

public static final NumberFormat getPercentInstance();

public static final NumberFormat getPercentInstance() method creates a NumberFormat that formats and parses percent values in the default locale.

This method returns a NumberFormat appropriate for the default Locale that formats percentage values.

public static NumberFormat getPercentInstance(Locale inLocale);

public static NumberFormat getPercentInstance(Locale inLocale) method creates a NumberFormat that formats and parses percent values in the given locale.

This method returns a NumberFormat appropriate for the given Locale that formats percentage values.

Parameter
inLocale – The Locale to use.

public Object clone();

public Object clone() method creates a copy of this NumberFormat and returns it.

This method returns a copy of this NumberFormat.

public boolean equals(Object obj);

public boolean equals(Object obj) method returns true if obj is an instance of NumberFormat and is equivalent to this
NumberFormat.

This method returns true if the objects are equal; false if they are not.

Parameter
obj – The object to be compared with this object.

public final String format(double number);

public final String format(double number) method formats the given number and returns the result as a string.

This method returns a string that contains a formatted representation of the value.

Parameter
number – The double value to be formatted.

public final String format(long number);

public final String format(long number) method formats the given number and returns the result as a string.

This method returns a string that contains a formatted representation of the value.

Parameter
number – The long value to be formatted.

public final StringBuffer format(Object number, StringBuffer toAppendTo,FieldPosition pos);

public final StringBuffer format(Object number, StringBuffer toAppendTo, FieldPosition pos) method formats the given object and appends the result to the given StringBuffer. If pos refers to one of the number fields, it’s beginning and ending indices are filled with the beginning and ending positions of the given field in the resulting formatted string.

READ  Learn Servlets in an Easy and Fast way

This method returns the given buffer toAppendTo with the formatted representation of the object appended to it.

Parameter
number – The object to be formatted.
toAppendTo – A StringBuffer on which to append the formatted information.
pos – A number field.

public abstract StringBuffer format(double number,StringBuffer toAppendTo, FieldPosition pos);

public abstract StringBuffer format(double number,StringBuffer toAppendTo, FieldPosition pos) method formats the given number and appends the result to the given StringBuffer. If pos refers to one of the number fields, its beginning and ending indices are filled with the beginning and ending positions of the given field in the resulting formatted string.

This method returns the given buffer toAppendTo with the formatted representation of the object appended to it.

Parameter
number – The double value to be formatted.
toAppendTo – A StringBuffer on which to append the formatted information.
pos – A number field.

public abstract StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos);

public abstract StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos) method formats the given number and appends the result to the given StringBuffer. If pos refers to one of the number fields, its beginning and ending indices are filled with the beginning and ending positions of the given field in the resulting formatted string.

This method returns the given buffer toAppendTo with the formatted representation of the object appended to it.

Parameter
number – The long value to be formatted.
toAppendTo – A StringBuffer on which to append the formatted information.
pos – A number field.

public int getMaximumFractionDigits();

public int getMaximumFractionDigits() method returns the maximum number of digits that can be in the fraction part of the number.

This method returns the maximum number of digits allowed in the fraction portion.

public int getMaximumIntegerDigits();

public int getMaximumIntegerDigits() method returns the maximum number of digits that can be in the integer part of the number.

This method returns the maximum number of digits allowed in the integer portion.

public int getMinimumFractionDigits();

public int getMinimumFractionDigits() method returns the minimum number of digits that can be in the fraction part of the number.

This method returns the minimum number of digits allowed in the fraction portion.

public int getMinimumIntegerDigits();

public int getMinimumIntegerDigits() method returns the minimum number of digits that can be in the integer part of the number.

This method returns the minimum number of digits allowed in the integer portion.

public int hashCode();

public int hashCode() method returns a hashcode for this NumberFormat.

This method returns a hashcode for this object.

public boolean isGroupingUsed();

public boolean isGroupingUsed() method returns true if this NumberFormat uses a grouping character. For example, it is common in the United States to use a comma as a grouping character: 1,234.56.

This method returns a boolean value that indicates whether or not this NumberFormat uses a grouping character to break up long sequences of digits in the integer part of a number.

public boolean isParseIntegerOnly();

public boolean isParseIntegerOnly() method returns true if this NumberFormat parses only integers.

This method returns a boolean value that indicates whether or not this NumberFormat parses only integers.

public Number parse(String text);

public Number parse(String text) method parses a number from the given string, starting from the beginning of the string.

This method returns the Number object represented by the given string.

Parameter
text – The string to be parsed.

public abstract Number parse(String text, ParsePosition parsePosition);

public abstract Number parse(String text, ParsePosition parsePosition) method parses a number from the given string, starting from the given position. After the string has been parsed, the given ParsePosition object is updated so that its index is after the parsed text.

READ  Empty Statements in Java

This method returns the Number object represented by the text starting at the given position.

Parameter
text – The string to be parsed.
parsePosition – A ParsePosition object that specifies a position in the string.

public final Object parseObject(String source, ParsePosition parsePosition);

public final Object parseObject(String source, ParsePosition parsePosition) method parses a number from the given string, starting from the given position. After the string has been parsed, the given ParsePosition object is updated so that its index is after the parsed text.

This method returns the object represented by the text starting at the given position.

Parameter
source – The string to be parsed.
parsePosition – A ParsePosition object that specifies a position in the string.

public void setGroupingUsed(boolean newValue);

public void setGroupingUsed(boolean newValue) method sets whether or not this NumberFormat uses a grouping character to break up long sequences of digits in the integer part of a number. For example, it is common in the United States to use a comma as a grouping character: 1,234.56.

Parameter
newValue – The new grouping flag.

public void setMaximumFractionDigits(int newValue);

public void setMaximumFractionDigits(int newValue) method sets the maximum number of digits that may be present in the fraction part of the number. The maximum value must be greater than the minimum number of fraction digits allowed. If the value is less than the current minimum, the minimum is also set to this value.

Parameter
newValue – The new maximum number of fraction digits.

public void setMaximumIntegerDigits(int newValue);

public void setMaximumIntegerDigits(int newValue) method sets the maximum number of digits that may be present in the integer part of the number. The maximum value must be greater than the minimum number of integer digits allowed. If the value is less than the current minimum, the minimum is also set to this value.

Parameter
newValue – The new maximum number of integer digits.

public void setMinimumFractionDigits(int newValue);

public void setMinimumFractionDigits(int newValue) method sets the minimum number of digits that may be present in the fraction part of the number. The minimum value must be less than the maximum number of fraction digits allowed. If the value is greater than the current maximum, the maximum is also set to this value.

Parameter
newValue – The new minimum number of fraction digits.

public void setMinimumIntegerDigits(int newValue);

public void setMinimumIntegerDigits(int newValue) method sets the minimum number of digits that may be present in the integer part of the number. The minimum value must be less than the maximum number of integer digits allowed. If the value is greater than the current maximum, the maximum is also set to this value.

Parameter
newValue -The new minimum number of integer digits.

public void setParseIntegerOnly(boolean value);

public void setParseIntegerOnly(boolean value) method sets whether or not this NumberFormat parses only integers. If the value is true, this NumberFormat only parses integers. Otherwise, it parses both the integer and fractional parts of numbers.

Parameter
value – The new parsing flag.

Apart from these methods, the NumberFormat class also has inherited methods from class- Object. They are as follows:

  • finalize()
  • notifyAll()
  • wait()
  • wait(long, int)
  • getClass()
  • notify()
  • toString()
  • wait(long)

The NumberFormat class also has inherited methods from Format. They are as follows:

  • format(Object)
  • parseObject(String)

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *