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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

The variant is for vendor-specific extensions and is rarely used. The only encodings in Table<br />

16.1 that list variants are the two Norwegian encodings, Norwegian (Bokmål) and Norwegian<br />

(Nynorsk). The Nynorsk encoding has a variant code of "NY". It is permissible to provide<br />

<strong>Java</strong> more information than it needs. If <strong>Java</strong> cannot find the variant locale you request, it<br />

provides a locale that only matches the language and country. If it cannot find a locale that<br />

matches the language and the country, it will settle for one that matches the language.<br />

16.3 Number Formats<br />

To print a formatted number in <strong>Java</strong>, perform these two steps:<br />

1. Format the number as a string.<br />

2. Print the string.<br />

Simple, right? Of course, this is a little like the old recipe for rabbit stew:<br />

1. Catch a rabbit.<br />

2. Boil rabbit in pot with vegetables and spices.<br />

Obviously, step 1 is the tricky part. Fortunately, formatting numbers as strings is somewhat<br />

easier than catching a rabbit. The key class that formats numbers as strings is<br />

java.text.NumberFormat. This is an abstract subclass of java.text.Format. Concrete<br />

subclasses such as java.text.DecimalFormat implement formatting policies for particular<br />

kinds of numbers.<br />

public abstract class NumberFormat extends Format implements Cloneable<br />

The static NumberFormat.getAvailableLocales() method returns a list of all locales<br />

installed that provide number formats. (There may be a few locales installed that only provide<br />

date or text formats, not number formats.)<br />

public static Locale[] getAvailableLocales()<br />

You can request a NumberFormat object for the default locale of the host computer or for one<br />

of the specified locales in Table 16.1 using the static NumberFormat.getInstance() method.<br />

For example:<br />

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

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

Locale turkey = new Locale("tr", "");<br />

NumberFormat turkishFormat = NumberFormat.getInstance(turkey);<br />

Locale swissItalian = new Locale("it", "CH");<br />

NumberFormat swissItalianFormat = NumberFormat.getInstance(swissItalian);<br />

The number format returned by NumberFormat.getInstance() should do a reasonable job<br />

of formatting most numbers. However, there's at least a theoretical possibility that the<br />

instance returned will format numbers as currencies or percentages. Therefore, it wouldn't<br />

hurt to use NumberFormat.getNumberInstance() instead:<br />

public static final NumberFormat getNumberInstance()<br />

public static NumberFormat getNumberInstance(Locale inLocale)<br />

400

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

Saved successfully!

Ooh no, something went wrong!