23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

For example:<br />

<strong>Java</strong> I/O<br />

NumberFormat myFormat = NumberFormat.getNumberInstance();<br />

NumberFormat canadaFormat = NumberFormat.getNumberInstance(Locale.CANADA);<br />

16.3.1 Formatting Numbers<br />

Once you've got a NumberFormat object, you can convert integers and floating point numbers<br />

into formatted strings using one of NumberFormat's five overloaded format methods:<br />

public final String format(long number)<br />

public final String format(double number)<br />

public abstract StringBuffer format(long number, StringBuffer toAppendTo,<br />

FieldPosition pos)<br />

public abstract StringBuffer format(double number, StringBuffer toAppendTo,<br />

FieldPosition pos)<br />

public final StringBuffer format(Object number, StringBuffer toAppendTo,<br />

FieldPosition pos)<br />

These methods all read the number and return a string or a modified string buffer based on<br />

that number using the number format's default formatting rules. These rules generally specify:<br />

• Maximum and minimum integer width<br />

• Maximum and minimum fraction width (precision, number of decimal places)<br />

• Whether or not digits are grouped (e.g., 2,109,356 versus 2109356)<br />

For any given number format, these rules can be quite complex. For instance, they may or<br />

may not take into account different digit sets, exponential or scientific notation, Roman<br />

numerals, or more. By creating new subclasses of NumberFormat, you can specify arbitrarily<br />

complex rules for converting binary numbers into strings. Regardless of exactly how a<br />

number format formats numbers, they are all manipulated the same way.<br />

The last three format() methods append the string to the specified StringBuffer<br />

toAppendTo. They then return that modified string buffer. They use a<br />

java.text.FieldPosition object to provide information to the client programmer about<br />

where the different parts of the number fall. This will be discussed later. The final format()<br />

method is used to format instances of the numeric type wrapper classes; that is,<br />

java.lang.Double, java.lang.Float, java.lang.Long, java.lang.Integer,<br />

java.lang.Short, java.lang.Character, and java.lang.Byte. It merely determines the<br />

type of the argument and passes either the double or long value of the argument to the<br />

format() method. Passing any other kind of object to this method will cause an<br />

IllegalArgumentException.<br />

Example 16.1 is about the simplest use of NumberFormat imaginable. It uses the default<br />

number format for the default locale to print multiples of . For comparison, both the<br />

formatted and unformatted numbers are printed.<br />

401

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!