Name ___________________ Section time _____________ Section instructor _________________
Introduction
We show you how to format decimal numbers (e.g. 5,000.253), currencies (e.g. $5,000.25), and percentages (e.g. 12.5%). We do more than this; we show you how to do it for many different cultures. For example, here's how people in two other parts of the world format these items.
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% |
People in the US sometimes think that they dominate the world,
but they are in the minority when it comes to many issues, even formatting decimal
numbers and percentages.
It is possible in Java to describe your own format for decimal numbers, but we don't show that in this lab. We just how to get the standard formats for currencies, percentages, and numbers.
The usefulness of object-orientation
We are showing you not Java language features but Java classes that have been developed and that come with any Java implementation. A class hierachy has been implemented that allows you to use the format of over 100 different countries and cultures. In fact, in a GUI, you could have users select the culture whose format they want to use. The use of classes and objects in this way makes Java truly an international programming language.
Step 0. Start a Java project.
Get a blank piece of paper to write some things.
Create a new folder on your hard drive. Bring up the course webpage in a browser,
click on "Labs" in the lefthand column, and download the two files
JLiveWindow.java
and MyJLiveWindow.java
for lab 09 to your new folder. Open MyJLiveWindow.java
in DrJava
and compile. In the interactions pane, execute the method call MyJLiveWindow.main(null);
.
In the GUI that comes up, type a number into the double field, select a "locale" using the "choice box" at the top of the GUI, and hit button Ready. The number will be printed in three ways in the String fields, all for the chosen locale: number format, currency format, and percentage format.
Step 1. Look at the formats of different locales.
Take a look at methods buttonPressed
and print3Fields
of class MyJLiveWindow
. You will see how a number like 5000.365
(placed in double
field 0 of the GUI) is printed in the decimal
format, currency, and percentage format of the localse selected in the choice
box at the top of the GUI. The default locale is English (U.S.), but you can
use the choice box to select one of over 100 locales.
Execute the program and write down on your paper the number 5000.365 in the formats for four different locales. First, English (U.S.), then English (United Kingdom), then Chinese (Taiwan), then one of your own choice. Be sure to hit the ready button after selecting a locale. You may get a question-mark instead of a currency symbol for some locales; that is because your computer may not know how to display that particular symbol.
The neat thing is that one can write a GUI for performing some service --provide interest calculations, for example-- and let the USER choose the culture (or locale) to be used to display the numbers.
Step 2. A look at locales.
A locale consists (essentially) of a language and a country. Class Locale
,
in package java.util
, contains information about over 100 locales
(but not all possible locales in the world). The English (U.S.) locale is the
default in the U.S., but you can probably set the default to what you want on
your computer.
To display the default locale of your computer in String
field 3 of the GUI, in method buttonPressed
, just before the return
statement, add
setStringField(3, "Default locale: " + Locale.getDefault());
Note that method getDefault
is a static
method of class Locale
, so it can be called without creating an instance of class Locale
. Method getDefault
does exactly what it sounds like --it gets the default locale for this computer. See the on-line Java API for more information on class Locale
.
When the program is run, the displayable form of the default locale for your computer is placed in String field 3, showing you the language and country, each in two letters. For example, if the default locale for your computer was French (Canada), field 3 would contain:
Default locale: fr_CA
The language code is defined by the international standard ISO-639; the country code, by ISO-3166.
Now add code to print the displayable form of the locale that you selected in the GUI by adding the following statement at the end of method buttonPressed
, just before the return
statement
setStringField(4, "Selected locale: " + getLocale());
Note that method getLocale
is inherited from superclass JLiveWindow
.
Write the display form of three different locales on your piece of paper. Show what was in the locale choice box as well as the language and country codes.
Step 3. Decimal number format.
Now suppose you want to format some decimal numbers using the format of
Locale loc
--i.e. the locale whose name is
in a variable loc
. To do this,
you first need an instance of the class that does such formatting, which you obtain using:
NumberFormat f= NumberFormat.getInstance(getLocale());
You then call a method to format the number that is in double field 0:
String s= f.format(getDoubleField(0));
And you can put the result in String field 5:
setStringField(5, s);
Place these three statements just before the return statement in method
buttonPressed
--don't delete any statements. Then execute the
program. You'll see that the values in String field 0 and 5 are the same,
because we use the same technique to convert the number into decimal format.
Place the following numbers in double
field 0, hitting the ready button
(with locale US selected, please) each
time, and write the resulting numbers that appear in String field 5 on your piece of
paper:
1E6
1E-6
3.1459467
3.1459467E3
3.1459467E15
Write on your piece of paper an English description of the standard US format for decimal numbers, based on these results.
Step 4. Currency format and percent format.
Above, you used NumberFormat.getInstance(getLocale());
to get
an instance of a class that would help you format numbers in the decimal format
of the selected locale. To get an instance of a class to format numbers in a
currency or as a percentage, use the statement
f= NumberFormat.getCurrencyInstance(getLocale());
or
f= NumberFormat.getPercentInstance(getLocale());
Try them out!
Step 5. Getting the available locales.
Not all possible locales are implemented by Java, but over 100 are. Take a look at the declaration of instance variable locales
in class JliveWindow
. Its righthand side is a call on a static
method in class Calendar
which returns an array of all locales that have been implemented. Variable locales
is used later in method createChoiceBox
to provide a list of all available locales.
It might be useful to have a listing of what the decimal, currency, or percent format of a particular number would look like for all locales at once.
Method printAllLocales
in class MyJLiveWindow
will print a list of all possible decimal formats of a double
variable, when int
parameter j
is greater than zero. Write a call to this method inside method buttonPressed
, so that it will print a list of all possible currency formats for the value entered in double
field 0.
The output will appear in the Java console, because of the use of the
System.out.println
statements.