Java Reference
In-Depth Information
decform.setDecimalSeparatorAlwaysShown( true );
Thereafter, decform1.format(25) is "25."
5.5.2
Formatting in locales
It is a shock to many Americans to learn that other cultures throughout the world
write numbers differently. For example, here is how Americans, the English, and
Russians write decimal numbers, currencies, and percentages:
Lab 3 of lesson
6 contains a
guided tour
through the use
of locales.
US English Russian
1,500,012.253 1.500.012,253 1 500 012,253
$1,500.23 1.500.23 1 500,23
100,075% 100,075% 100 075%
For decimal numbers, the Americans and English give opposite roles to the
comma and period! The Russians, like the English, have a “decimal comma”
instead of a “decimal point” but use a space to separate groupings of digits.
It would be nice to be able to write numbers in the format of any culture. For
example, a GUI could let a user indicate which format to use. This is easy to do
in Java.
Class Locale
A locale consists of a language and country. Class Locale , in package
java.util , contains information about 140 locales (but not all possible locales
in the world). The English-U.S. locale is the default in most computers in the
U.S, but you can probably set the default to what you want on your computer. To
see what the default is on your computer, type this expression in DrJava's
Interactions pane:
Locale.getDefault()
On a computer in the U.S., the value is "en_US" , meaning that the language
is English and the country is the U.S. If the default locale for your computer is
French-Canada, it will print "fr_CA" . The first two letters describe the language,
using a code defined by the international standard ISO-639. (ISO stands for
International Standards Organization). The last two letters describe the country,
using a code defined by the international standard ISO-3166.
Class Locale has over 50 static constants that are instances of class Locale .
For example, one of them is declared as:
static public final Locale KOREA = new Locale("ko","KR","");
Conversion using a Locale
Class NumberFormat , in package java.text , can be used to convert deci-
mal numbers using the conventions of a particular Locale . Suppose you have
placed an instance of Locale —one of the predefined ones— in variable locale .
Search WWH ::




Custom Search