Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 672
نمايش فراداده

NumberFormatjava.text

Java 1.1cloneable serializable

This 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 you 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. getIntegerInstance( ), getCurrencyInstance( ), and getPercentInstance( ) return NumberFormat objects for formatting numbers that are integers, or represent monetary amounts or percentages. These methods return a NumberFormat suitable for the default locale, or for the specified Locale object. getAvailableLocales( ) returns an array of locales for which NumberFormat objects are available. In Java 1.4 and later, use setCurrency( ) to provide a java.util.Currency object for use when formating monetary values. Note that 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.

Once you have created a suitable NumberFormat object, you can customize its localeindependent behavior with setMaximumFractionDigits( ), setGroupingUsed( ), and similar set methods. In order to customize the locale-dependent behavior, you can use instanceof to test if the NumberFormat object is an instance of DecimalFormat, and, if so, 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, you can use the various format( ) methods to convert numbers to strings or string buffers, and you can use the parse( ) or parseObject( ) methods to convert strings to numbers. You can also use the formatToCharacterIterator( ) method inherited from Format (and overridden by DecimalFormat) in place of format( ). The constants defined by this class are to be used by the FieldPosition object.

Figure 15-13. java.text.NumberFormat

public abstract class

NumberFormat extends Format { // Public Constructors public

NumberFormat ( ); // Public Constants public static final int

FRACTION_FIELD ; =1 public static final int

INTEGER_FIELD ; =0 // Nested Types

1.4 public static class

Field extends Format.Field; // Public Class Methods public static java.util.Locale[ ]

getAvailableLocales ( ); public static final NumberFormat

getCurrencyInstance ( ); public static NumberFormat

getCurrencyInstance (java.util.Locale

inLocale ); public static final NumberFormat

getInstance ( ); public static NumberFormat

getInstance (java.util.Locale

inLocale );

1.4 public static final NumberFormat

getIntegerInstance ( );

1.4 public static NumberFormat

getIntegerInstance (java.util.Locale

inLocale ); public static final NumberFormat

getNumberInstance ( ); public static NumberFormat

getNumberInstance (java.util.Locale

inLocale ); public static final NumberFormat

getPercentInstance ( ); public static NumberFormat

getPercentInstance (java.util.Locale

inLocale ); // Public Instance Methods public final String

format (long

number ); public final String

format (double

number ); public abstract StringBuffer

format (long

number , StringBuffer

toAppendTo , FieldPosition

pos ); public abstract StringBuffer

format (double

number , StringBuffer

toAppendTo , FieldPosition

pos );

1.4 public java.util.Currency

getCurrency ( ); public int

getMaximumFractionDigits ( ); public int

getMaximumIntegerDigits ( ); public int

getMinimumFractionDigits ( ); public int

getMinimumIntegerDigits ( ); public boolean

isGroupingUsed ( ); public boolean

isParseIntegerOnly ( ); public Number

parse (String

source ) throws ParseException; public abstract Number

parse (String

source , ParsePosition

parsePosition );

1.4 public void

setCurrency (java.util.Currency

currency ); 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 ); // Public Methods Overriding Format public Object

clone ( ); public StringBuffer

format (Object

number , StringBuffer

toAppendTo , FieldPosition

pos ); public final Object

parseObject (String

source , ParsePosition

pos ); // Public Methods Overriding Object public boolean

equals (Object

obj ); public int

hashCode ( ); }

Subclasses

ChoiceFormat, DecimalFormat

Passed To

DateFormat.setNumberFormat( )

Returned By

DateFormat.getNumberFormat( )

Type Of

DateFormat.numberFormat