Docsity
Docsity

Prepare-se para as provas
Prepare-se para as provas

Estude fácil! Tem muito documento disponível na Docsity


Ganhe pontos para baixar
Ganhe pontos para baixar

Ganhe pontos ajudando outros esrudantes ou compre um plano Premium


Guias e Dicas
Guias e Dicas


Capítulo 36 - Internacionalización - Liang-Java-Comp11e, Resumos de Informática

Java Internacionalización

Tipologia: Resumos

2018

Compartilhado em 21/07/2018

dddmmm80-5
dddmmm80-5 🇧🇷

4.8

(4)

13 documentos

1 / 33

Toggle sidebar

Esta página não é visível na pré-visualização

Não perca as partes importantes!

bg1
Objectives
To describe Java’s internationalization features (§36.1).
To construct a locale with language, country, and variant (§36.2).
To display date and time based on locale (§36.3).
To display numbers, currencies, and percentages based on locale (§36.4).
To develop applications for international audiences using resource
bundles (§36.5).
To specify encoding schemes for text I/O (§36.6).
Internationalization
CHAPTER
36
M36_LIAN0182_11_SE_C36.indd 1 5/29/17 9:29 AM
© 2018 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21

Pré-visualização parcial do texto

Baixe Capítulo 36 - Internacionalización - Liang-Java-Comp11e e outras Resumos em PDF para Informática, somente na Docsity!

Objectives

■ ■ To describe Java’s internationalization features (§36.1).

■ ■ To construct a locale with language, country, and variant (§36.2).

■ ■ To display date and time based on locale (§36.3).

■ ■ To display numbers, currencies, and percentages based on locale (§36.4).

■ ■ To develop applications for international audiences using resource

bundles (§36.5).

■ ■ To specify encoding schemes for text I/O (§36.6).

Internationalization

CHAPTER

36-2 Chapter 36 Internationalization

36.1 Introduction

This chapter introduces writing Java code for international audience.

Many websites maintain several versions of webpages so that readers can choose one written

in a language they understand. Because there are so many languages in the world, it would be

highly problematic to create and maintain enough different versions to meet the needs of all cli-

ents everywhere. Java comes to the rescue. Java is the first language designed from the ground

up to support internationalization. In consequence, it allows your programs to be customized

for any number of countries or languages without requiring cumbersome changes in the code.

Here are the major Java features that support internationalization:

■ ■ Java characters use Unicode , a 16-bit encoding scheme established by the Unicode

Consortium to support the interchange, processing, and display of written texts in the

world’s diverse languages. The use of Unicode encoding makes it easy to write Java

programs that can manipulate strings in any international language. (To see all the

Unicode characters, visit mindprod.com/jgloss/reuters.html.)

■ ■ Java provides the Locale class to encapsulate information about a specific locale. A

Locale object determines how locale-sensitive information, such as date, time, and

number, is displayed, and how locale-sensitive operations, such as sorting strings, are

performed. The classes for formatting date, time, and numbers, and for sorting strings

are grouped in the java.text package.

■ ■ Java uses the ResourceBundle class to separate locale-specific information, such

as status messages and GUI component labels, from the program. The information is

stored outside the source code and can be accessed and loaded dynamically at runtime

from a ResourceBundle , rather than hard-coded into the program.

In this chapter, you will learn how to format dates, numbers, currencies, and percentages for

different regions, countries, and languages. You will also learn how to use resource bundles

to define which images and strings are used by a component, depending on the user’s locale

and preferences.

36.2 The Locale Class

The Locale class defines a locale: language and nation.

A Locale object represents a geographical, political, or cultural region in which a specific

language or custom is used. For example, Americans speak English, and the Chinese speak

Chinese. The conventions for formatting dates, numbers, currencies, and percentages may dif-

fer from one country to another. The Chinese, for instance, use year/month/day to represent the

date, while Americans use month/day/year. It is important to realize that locale is not defined

only by country. For example, Canadians speak either Canadian English or Canadian French,

depending on which region of Canada they reside in.

To create a Locale object, use one of the three constructors with a specified language and

optional country and variant, as shown in Figure 36.1.

The language should be a valid language code—that is, one of the lowercase two-letter

codes defined by ISO-639. For example, zh stands for Chinese, da for Danish, en for English,

de for German, and ko for Korean. Table 36.1 lists the language codes.

The country should be a valid ISO country code—that is, one of the uppercase, two-letter

codes defined by ISO-3166. For example, CA stands for Canada, CN for China, DK for Denmark,

DE for Germany, and US for the United States. Table 36.2 lists the country codes.

The argument variant is rarely used and is needed only for exceptional or system-dependent

situations to designate information specific to a browser or vendor. For example, the Norwe-

gian language has two sets of spelling rules, a traditional one called bokmål and a new one

called nynorsk. The locale for traditional spelling would be created as follows:

new Locale( "no" , "NO" , "B" );

Point

Key

Point

Key

36-4 Chapter 36 Internationalization

The Locale class also provides the following constants based on language:

Locale.CHINESE , Locale.ENGLISH , Locale.FRENCH , Locale.GERMAN , Locale.ITALIAN , Locale.JAPANESE , Locale.KOREAN , Locale.SIMPLIFIED_CHINESE , and Locale.TRADITIONAL_CHINESE

Tip You can invoke the static method getAvailableLocales() in the Locale class to obtain all the available locales supported in the system. For example,

Locale[] availableLocales = Calendar.getAvailableLocales();

returns all the locales in an array.

Tip Your machine has a default locale. You may override it by supplying the language and region parameters when you run the program, as follows:

java –Duser.language=zh –Duser.region=CN MainClass

An operation that requires a Locale to perform its task is called locale sensitive. Displaying a

number such as a date or time, for example, is a locale-sensitive operation; the number should

be formatted according to the customs and conventions of the user’s locale. The sections that

follow introduce locale-sensitive operations.

36.2.1 How does Java support international characters in languages like Chinese and Arabic? 36.2.2 How do you construct a Locale object? How do you get all the available locales

from a Calendar object?

36.2.3 How do you create a locale for the French-speaking region of Canada? How do you

create a locale for the Netherlands?

36.3 Displaying Date and Time

The representation of date and time is dependent on locale.

Applications often need to obtain date and time. Java provides a system-independent encapsu-

lation of date and time in the java.util.Date class; it also provides java.util.TimeZone

for dealing with time zones, and java.util.Calendar for extracting detailed informa-

tion from Date. Different locales have different conventions for displaying date and time.

Should the year, month, or day be displayed first? Should slashes, periods, or colons be used

to separate fields of the date? What are the names of the months in the language? The

java.text.DateFormat class can be used to format date and time in a locale-sensitive way

for display to the user. The Date class was introduced in Section 9.6.1, “The Date Class,” and

the Calendar class and its subclass GregorianCalendar were introduced in Section 13.4,

“Case Study: Calendar and GregorianCalendar .”

36.3.1 The TimeZone Class

TimeZone represents a time zone offset and also figures out daylight savings. To get a

TimeZone object for a specified time zone ID, use TimeZone.getTimeZone(id). To set a

time zone in a Calendar object, use the setTimeZone method with a time zone ID. For exam-

ple, cal.setTimeZone(TimeZone.getTimeZone("CST")) sets the time zone to Central

Standard Time. To find all the available time zones supported in Java, use the static method

getAvailableIDs( ) in the TimeZone class. In general, the international time zone ID is a

string in the form of continent/city like Europe/Berlin, Asia/Taipei, and America/Washington.

You can also use the static method getDefault() in the TimeZone class to obtain the default

time zone on the host machine.

Point

Check

Point

Key

36.3 Displaying Date and Time 36-

36.3.2 The DateFormat Class

The DateFormat class can be used to format date and time in a number of styles. The

DateFormat class supports several standard formatting styles. To format date and time, simply

create an instance of DateFormat using one of the three static methods getDateInstance ,

getTimeInstance , and getDateTimeInstance and apply the format(Date) method on

the instance, as shown in Figure 36.2.

java.text.DateFormat +format(date: Date): String +getDateInstance(): DateFormat +getDateInstance(dateStyle: int): DateFormat +getDateInstance(dateStyle: int, aLocale: Locale): DateFormat +getDateTimeInstance(): DateFormat

+getDateTimeInstance(dateStyle: int, timeStyle: int): DateFormat +getDateTimeInstance(dateStyle: int, timeStyle: int, aLocale: Locale): DateFormat +getInstance(): DateFormat

Formats a date into a date/time string. Gets the date formatter with the default formatting style for the default locale. Gets the date formatter with the given formatting style for the default locale. Gets the date formatter with the given formatting style for the given locale.

Gets the date and time formatter with the default formatting style for the default locale. Gets the date and time formatter with the given date and time formatting styles for the default locale. Gets the date and time formatter with the given formatting styles for the given locale. Gets a default date and time formatter that uses the SHORT style for both the date and the time.

Figure 36.2 The DateFormat class formats date and time.

The dateStyle and timeStyle are one of the following constants: DateFormat.SHORT ,

DateFormat.MEDIUM , DateFormat.LONG , DateFormat.FULL. The exact result depends

on the locale, but generally,

■ ■ SHORT is completely numeric, such as 7/24/98 (for date) and 4:49 PM (for time).

■ ■ MEDIUM is longer, such as 24-Jul-98 (for date) and 4:52:09 PM (for time).

■ ■ LONG is even longer, such as July 24, 1998 (for date) and 4:53:16 PM EST (for time).

■ ■ FULL is completely specified, such as Friday, July 24, 1998 (for date) and 4:54:

o’clock PM EST (for time).

The statements given below display current time with a specified time zone (CST), format-

ting style (full date and full time), and locale (US).

GregorianCalendar calendar = new GregorianCalendar(); DateFormat formatter = DateFormat.getDateTimeInstance( DateFormat.FULL, DateFormat.FULL, Locale.US); TimeZone timeZone = TimeZone.getTimeZone( "CST" ); formatter.setTimeZone(timeZone); System.out.println( "The local time is " + formatter.format(calendar.getTime()));

36.3.3 The SimpleDateFormat Class

The date and time formatting subclass, SimpleDateFormat , enables you to choose any user-

defined pattern for date and time formatting. The constructor shown below can be used to

create a SimpleDateFormat object, and the object can be used to convert a Date object into

a string with the desired format.

public SimpleDateFormat(String pattern)

36.3 Displaying Date and Time 36-

Figure 36.4 The program displays a clock that shows the current time with the specified

locale and time zone.

Figure 36.5 WorldClockApp contains WorldClockControl , and WorldClockControl contains WorldClock.

WorldClockApp

+start(primaryStage: Stage) : void +main(args: String[]): void

javafx.application.Application

WorldClockControl -clock: WorldClock -cboLocales: ComboBox -cboTimeZones: ComboBox -availableLocales: Locale[] -availableTimeZones: String[] +WorldClockControl() -setAvailableLocales(): void -setAvailableTimeZones(): void

WorldClock -timeZone: TimeZone -locale: Locale -clock: ClockPane -lblDigitTime: Label

+WorldClock() +setTimeZone(timeZone: TimeZone): void +setLocale(locale: Locale): void -setCurrentTime(): void

1 1 1 1

javafx.scene.layout.BorderPane javafx.scene.layout.BorderPane

Here are the major steps in the program:

1. Create a subclass of BorderPane named WorldClock (see Listing 36.1) to contain an

instance of the ClockPane class (developed in Listing 14.21, ClockPane.java), and place

it in the center. Create a Label to display the digit time, and place it in the bottom. Use the

GregorianCalendar class to obtain the current time for a specific locale and time zone.

2. Create a subclass of BorderPanel named WorldClockControl (see Listing 36.2)

to contain an instance of WorldClock and two instances of ComboBox for selecting

locales and time zones.

3. Create an application named WorldClockApp (see Listing 36.3) to display an instance

of WorldClockControl.

The relationship among these classes is shown in Figure 36.5.

lisTing 36.1 WorldClock.java

1 import java.util.Calendar; 2 import java.util.TimeZone; 3 import java.util.GregorianCalendar; 4 import java.text.*; 5 import java.util.Locale; 6 import javafx.animation.KeyFrame; 7 import javafx.animation.Timeline; 8 import javafx.event.ActionEvent; 9 import javafx.event.EventHandler; 10 import javafx.geometry.Pos; 11 import javafx.scene.control.Label;

36-8 Chapter 36 Internationalization

12 import javafx.scene.layout.BorderPane; 13 import javafx.util.Duration; 14 15 public class WorldClock extends BorderPane { 16 private TimeZone timeZone = TimeZone.getTimeZone( "EST" ); 17 private Locale locale = Locale.getDefault(); 18 private ClockPane clock = new ClockPane(); // Still clock 19 private Label lblDigitTime = new Label(); 20 21 public WorldClock() { 22 setCenter(clock); 23 setBottom(lblDigitTime); 24 BorderPane.setAlignment(lblDigitTime, Pos.CENTER); 25 26 EventHandler eventHandler = e -> { 27 setCurrentTime(); // Set a new clock time 28 }; 29 30 // Create an animation for a running clock 31 Timeline animation = new Timeline( 32 new KeyFrame(Duration.millis( 1000 ), eventHandler)); 33 animation.setCycleCount(Timeline.INDEFINITE); 34 animation.play(); // Start animation 35 36 // Resize the clock 37 widthProperty().addListener(ov -> clock.setWidth(getWidth())); 38 heightProperty().addListener(ov -> clock.setHeight(getHeight())); 39 } 40 41 public void setTimeZone(TimeZone timeZone) { 42 this .timeZone = timeZone; 43 } 44 45 public void setLocale(Locale locale) { 46 this .locale = locale; 47 } 48 49 private void setCurrentTime() { 50 Calendar calendar = new GregorianCalendar(timeZone, locale); 51 clock.setHour(calendar.get(Calendar.HOUR)); 52 clock.setMinute(calendar.get(Calendar.MINUTE)); 53 clock.setSecond(calendar.get(Calendar.SECOND)); 54 55 // Display digit time on the label 56 DateFormat formatter = DateFormat.getDateTimeInstance 57 (DateFormat.MEDIUM, DateFormat.LONG, locale); 58 formatter.setTimeZone(timeZone); 59 lblDigitTime.setText(formatter.format(calendar.getTime())); 60 } 61 }

lisTing 36.2 WorldClockControl.java

1 import java.util.*; 2 import javafx.geometry.Pos; 3 import javafx.scene.control.ComboBox; 4 import javafx.scene.control.Label; 5 import javafx.scene.layout.BorderPane; 6 import javafx.scene.layout.GridPane; 7 8 public class WorldClockControl extends BorderPane {

36-10 Chapter 36 Internationalization

70 cboTimeZones.getSelectionModel().selectFirst(); 71 } 72 }

lisTing 36.3 WorldClockApp.java

1 import javafx.application.Application; 2 import javafx.scene.Scene; 3 import javafx.stage.Stage; 4 5 public class WorldClockApp extends Application { 6 @Override // Override the start method in the Application class 7 public void start(Stage primaryStage) { 8 // Create a scene and place it in the stage 9 Scene scene = new Scene( new WorldClockControl(), 450 , 350 ); 10 primaryStage.setTitle( "WorldClockApp" ); // Set the stage title 11 primaryStage.setScene(scene); // Place the scene in the stage 12 primaryStage.show(); // Display the stage 13 } 14 }

The WorldClock class creates an instance of ClockPane (line 18) and places it in the center

of the border pane (line 22). The setCurrentTime() method uses GregorianCalendar to

obtain a Calendar object for the specified locale and time zone (line 50). The clock time is

updated every one second using the current Calendar object in lines 51–53.

An instance of DateFormat is created (lines 56–57) and is used to format the date in

accordance with the locale (line 59).

The WorldClockControl class contains an instance of WorldClock and two combo

boxes. The combo boxes store all the available locales and time zones (lines 56–71). The newly

selected locale and time zone are set in the clock (lines 47–53) and used to display a new time

based on the current locale and time zone.

36.3.6 Example: Displaying a Calendar

Write a program that displays a calendar based on the specified locale, as shown in Figure 36.6.

The user can specify a locale from a combo box that consists of a list of all the available locales

supported by the system. When the program starts, the calendar for the current month of the

year is displayed. The user can use the Prior and Next buttons to browse the calendar.

Figure 36.6 The calendar program displays a calendar with a specified locale.

Here are the major steps in the program:

1. Define a subclass of BorderPane named CalendarPane (see Listing 36.4) to display

the calendar for the given year and month based on the specified locale.

36.3 Displaying Date and Time 36-

lisTing 36.4 CalendarPane.java

1 import java.text.DateFormatSymbols; 2 import java.text.SimpleDateFormat; 3 import java.util.Calendar; 4 import java.util.GregorianCalendar; 5 import java.util.Locale; 6 import javafx.geometry.Pos; 7 import javafx.scene.control.Label; 8 import javafx.scene.layout.BorderPane; 9 import javafx.scene.layout.GridPane; 10 import javafx.scene.paint.Color; 11 import javafx.scene.text.TextAlignment; 12 13 public class CalendarPane extends BorderPane { 14 // The header label 15 private Label lblHeader = new Label(); 16 17 // Maximum number of labels to display day names and days 18 private Label[] lblDay = new Label[ 49 ]; 19 20 private Calendar calendar; 21 private int month; // The specified month 22 private int year; // The specified year 23 private Locale locale = Locale.CHINA; 24 25 public CalendarPane() { 26 // Create labels for displaying days 27 for ( int i = 0 ; i < 49 ; i++) { 28 lblDay[i] = new Label(); 29 lblDay[i].setTextAlignment(TextAlignment.RIGHT);

Figure 36.7 CalendarApp contains CalendarPane.

CalendarPane

-month: int -year: int -calendar: java.util.Calendar -locale: Locale

+getMonth(): int +setMonth(newMonth: int): void +getYear(): int +setYear(newYear: int): void +setLocale(newLocale: Locale): void +showHeader(): void +showDayNames(): void +showDays(): void

CalendarApp

-calendarPane: CalendarPane -cboLocale: ComboBox -btPrior: Button -btNext: Button -locales: java.utilLocale[]

+start(primaryStage: Stage): void +main(args: String[]): void

javafx.scene.layout.BorderPane javafx.application.Application

1 1

2. Define an application named CalendarApp (Listing 36.5). Create a pane to hold an

instance of CalendarPane in the center, two buttons, Prior and Next in the bottom, and

a combo box in the top of the pane. The relationships among these classes are shown

in Figure 36.7.

36.3 Displaying Date and Time 36-

90 lblDay[i + 7 ].setText(daysInPrecedingMonth 91 - startingDayOfMonth + 2 + i + "" ); 92 } 93 94 // Display days of this month 95 int daysInCurrentMonth = calendar.getActualMaximum( 96 Calendar.DAY_OF_MONTH); 97 for ( int i = 1 ; i <= daysInCurrentMonth; i++) { 98 lblDay[i - 2 + startingDayOfMonth + 7 ].setTextFill(Color.BLACK); 99 lblDay[i - 2 + startingDayOfMonth + 7 ].setText(i + "" ); 100 } 101 102 // Fill the calendar with the days after this month 103 int j = 1 ; 104 for ( int i = daysInCurrentMonth - 1 + startingDayOfMonth + 7 ; 105 i < 49 ; i++) { 106 lblDay[i].setTextFill(Color.LIGHTGRAY); 107 lblDay[i].setText(j++ + "" ); 108 } 109 } 110 111 /** Set the calendar to the first day of the 112 * specified month and year 113 */ 114 public void updateCalendar() { 115 calendar.set(Calendar.YEAR, year); 116 calendar.set(Calendar.MONTH, month); 117 calendar.set(Calendar.DATE, 1 ); 118 } 119 120 public int getMonth() { 121 return month; 122 } 123 124 public void setMonth( int newMonth) { 125 month = newMonth; 126 updateCalendar(); 127 showHeader(); 128 showDays(); 129 } 130 131 public int getYear() { 132 return year; 133 } 134 135 public void setYear( int newYear) { 136 year = newYear; 137 updateCalendar(); 138 showHeader(); 139 showDays(); 140 } 141 142 public void setLocale(Locale locale) { 143 this .locale = locale; 144 updateCalendar(); 145 showDayNames(); 146 showHeader(); 147 showDays(); 148 } 149 }

36-14 Chapter 36 Internationalization

CalendarPane is created to control and display the calendar. It displays the month and year

in the header, and the day names and days in the calendar body. The header and day names

are locale sensitive.

The showHeader method (lines 71–76) displays the calendar title in a form like “MMMM

yyyy”. The SimpleDateFormat class used in the showHeader method is a subclass of

DateFormat. SimpleDateFormat allows you to customize the date format to display the

date in various nonstandard styles.

The showDayNames method (lines 60–68) displays the day names in the calendar. The

DateFormatSymbols class used in the showDayNames method is a class for encapsulating

localizable date-time formatting data, such as the names of the months, the names of the days of

the week, and the time-zone data. The getWeekdays method is used to get an array of day names.

The showDays method (lines 60–68) displays the days for the specified month of the year.

As you can see in Figure 36.6, the labels before the current month are filled with the last few

days of the preceding month, and the labels after the current month are filled with the first few

days of the next month.

To fill the calendar with the days before the current month, a clone of calendar ,

named cloneCalendar , is created to obtain the days for the preceding month (line 83).

cloneCalendar is a copy of calendar with separate memory space. Thus you can

change the properties of cloneCalendar without corrupting the calendar object. The

clone() method is defined in the Object class, which was introduced in Section 13.7,

“The Cloneable Interface.” You can clone any object as long as its defining class imple-

ments the Cloneable interface. The Calendar class implements Cloneable.

The cloneCalendar.getActualMaximum(Calendar.DAY_OF_MONTH) method (lines

95–96) returns the number of days in the month for the specified calendar.

lisTing 36.5 CalendarApp.java

1 import java.util.Locale; 2 import javafx.application.Application; 3 import javafx.geometry.Pos; 4 import javafx.scene.Scene; 5 import javafx.scene.control.Button; 6 import javafx.scene.control.ComboBox; 7 import javafx.scene.control.Label; 8 import javafx.scene.layout.BorderPane; 9 import javafx.scene.layout.HBox; 10 import javafx.stage.Stage; 11 12 public class CalendarApp extends Application { 13 private CalendarPane calendarPane = new CalendarPane(); 14 private Button btPrior = new Button( "Prior" ); 15 private Button btNext = new Button( "Next" ); 16 private ComboBox cboLocales = new ComboBox<>(); 17 private Locale[] availableLocales = Locale.getAvailableLocales(); 18 19 @Override // Override the start method in the Application class 20 public void start(Stage primaryStage) { 21 HBox hBox = new HBox( 5 ); 22 hBox.getChildren().addAll(btPrior, btNext); 23 hBox.setAlignment(Pos.CENTER); 24 25 // Initialize cboLocales with all available locales 26 setAvailableLocales(); 27 HBox hBoxLocale = new HBox( 5 ); 28 hBoxLocale.getChildren().addAll( 29 new Label( "Select a locale" ), cboLocales); 30 31 BorderPane pane = new BorderPane();

36-16 Chapter 36 Internationalization

36.4 Formatting Numbers

You can format numbers based on locales.

Formatting numbers is highly locale dependent. For example, number 5000.555 is displayed

as 5,000.555 in the United States, but as 5 000,555 in France and as 5.000,555 in Germany.

Numbers are formatted using the java.text.NumberFormat class, an abstract base class

that provides the methods for formatting and parsing numbers, as shown in Figure 36.8.

Point

Key

java.text.NumberFormat +getInstance(): NumberFormat +getInstance(locale: Locale): NumberFormat +getIntegerInstance(): NumberFormat

  • getIntegerInstance(locale: Locale): NumberFormat
  • getCurrencyInstance(): NumberFormat +getNumberInstance(): NumberFormat +getNumberInstance(locale: Locale): NumberFormat +getPercentInstance(): NumberFormat +getPercentInstance(locale: Locale): NumberFormat +format (number: double): String

+format (number: long): String

+getMaximumFractionDigits(): int

+setMaximumFractionDigits(newValue: int): void

+getMinimumFractionDigits(): int

+setMinimumFractionDigits(newValue: int): void

+getMaximumIntegerDigits(): int

+setMaximumIntegerDigits(newValue: int): void

+getMinimumIntegerDigits(): int

+setMinimumIntegerDigits(newValue: int): void +isGroupingUsed(): boolean

+setGroupingUsed(newValue: boolean): void +parse(source: String): Number +getAvailableLocales(): Locale[]

Returns a default number format for the default locale. Returns a default number format for the specified locale. Returns an integer number format for the default locale. Returns an integer number format for the specified locale.

Returns a currency format for the current default locale. Same as getInstance(). Same as getInstance(locale).

Returns a percentage format for the default locale. Returns a percentage format for the specified locale.

Formats a floating-point number.

Formats an integer.

Returns the maximum number of allowed fraction digits.

Sets the maximum number of allowed fraction digits.

Returns the minimum number of allowed fraction digits.

Sets the minimum number of allowed fraction digits.

Returns the maximum number of allowed integer digits in a fraction number.

Sets the maximum number of allowed integer digits in a fraction number.

Returns the minimum number of allowed integer digits in a fraction number.

Sets the minimum number of allowed integer digits in a fraction number. Returns true if grouping is used in this format. For example, in the English locale, with grouping on, the number 1234567 is formatted as "1,234,567". Sets whether or not grouping will be used in this format. Parses string into a number. Gets the set of locales for which NumberFormats are installed.

Figure 36.8 The NumberFormat class provides the methods for formatting and parsing numbers.

With NumberFormat , you can format and parse numbers for any locale. Your code will

be completely independent of locale conventions for decimal points, thousands-separators,

currency format, and percentage formats.

36.4.1 Plain Number Format

You can get an instance of NumberFormat for the current locale using NumberFormat.

getInstance() or NumberFormat.getNumberInstance and for the specified locale using

NumberFormat.getInstance(Locale) or NumberFormat.getNumberInstance(Locale).

36.4 Formatting Numbers 36-

You can then invoke format(number) on the NumberFormat instance to return a formatted

number as a string.

For example, to display number 5000.555 in France, use the following code:

NumberFormat numberFormat = NumberFormat.getInstance(Locale.FRANCE); System.out.println(numberFormat.format(5000.555));

You can control the display of numbers with such methods as setMaximumFractionDigits

and setMinimumFractionDigits. For example, 5000.555 will be displayed as 5000.6 if

you use numberFormat.setMaximumFractionDigits(1).

36.4.2 Currency Format

To format a number as a currency value, use NumberFormat.getCurrency-

Instance() to get the currency number format for the current locale or NumberFormat.

getCurrencyInstance(Locale) to get the currency number for the specified locale.

For example, to display number 5000.555 as currency in the United States, use the fol-

lowing code:

NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(Locale.US); System.out.println(currencyFormat.format(5000.555));

5000.555 is formatted into $5,000,56. If the locale is set to France, the number will be format-

ted into 5,000,56 €.

36.4.3 Percent Format

To format a number in a percent, use NumberFormat.getPercentInstance() or

NumberFormat.getPercentInstance(Locale) to get the percent number format for

the current locale or the specified locale.

For example, to display number 0.555367 as a percent in the United States, use the follow-

ing code:

NumberFormat percentFormat = NumberFormat.getPercentInstance(Locale.US); System.out.println(percentFormat.format(0.555367));

0.555367 is formatted into 56%. By default, the format truncates the fraction part in a percent

number. If you want to keep three digits after the decimal point, use percentFormat.

setMinimumFractionDigits(3). So 0.555367 would be displayed as 55.537%.

36.4.4 Parsing Numbers

You can format a number into a string using the format(numericalValue) method. You

can also use the parse(String) method to convert a formatted plain number, currency value,

or percent number with the conventions of a certain locale into an instance of java.lang.

Number. The parse method throws a java.text.ParseException if parsing fails. For

example, U.S. $5,000.56 can be parsed into a number using the following statements:

NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(Locale.US); try { Number number = currencyFormat.parse("$5,000.56"); System.out.println(number.doubleValue()); } catch (java.text.ParseException ex) { System.out.println( "Parse failed"); }

36.4 Formatting Numbers 36-

23 private TextField tfFormattedInterestRate = new TextField(); 24 private TextField tfFormattedNumberOfYears = new TextField(); 25 private TextField tfFormattedLoanAmount = new TextField(); 26 27 // Text fields for monthly payment and total payment 28 private TextField tfTotalPayment = new TextField(); 29 private TextField tfMonthlyPayment = new TextField(); 30 31 // Compute button 32 private Button btCompute = new Button( "Compute" ); 33 34 // Current locale 35 private Locale locale = Locale.getDefault(); 36 37 // Declare locales to store available locales 38 private Locale locales[] = Calendar.getAvailableLocales(); 39 40 /** Initialize the combo box */ 41 public void initializeComboBox() { 42 // Add locale names to the combo box 43 for ( int i = 0 ; i < locales.length; i++) 44 cboLocale.getItems().add(locales[i].getDisplayName()); 45 } 46 47 @Override // Override the start method in the Application class 48 public void start(Stage primaryStage) { 49 initializeComboBox(); 50 51 // Pane to hold the combo box for selecting locales 52 HBox hBox = new HBox( 5 ); 53 hBox.getChildren().addAll( 54 new Label( "Choose a Locale" ), cboLocale); 55 56 // Pane to hold the input 57 GridPane gridPane = new GridPane(); 58 gridPane.add( new Label( "Interest Rate" ), 0 , 0 ); 59 gridPane.add(tfInterestRate, 1 , 0 ); 60 gridPane.add(tfFormattedInterestRate, 2 , 0 ); 61 gridPane.add( new Label( "Number of Years" ), 0 , 1 ); 62 gridPane.add(tfNumberOfYears, 1 , 1 ); 63 gridPane.add(tfFormattedNumberOfYears, 2 , 1 ); 64 gridPane.add( new Label( "Loan Amount" ), 0 , 2 ); 65 gridPane.add(tfLoanAmount, 1 , 2 ); 66 gridPane.add(tfFormattedLoanAmount, 2 , 2 ); 67 68 // Pane to hold the output 69 GridPane gridPaneOutput = new GridPane(); 70 gridPaneOutput.add( new Label( "Monthly Payment" ), 0 , 0 ); 71 gridPaneOutput.add(tfMonthlyPayment, 1 , 0 ); 72 gridPaneOutput.add( new Label( "Total Payment" ), 0 , 1 ); 73 gridPaneOutput.add(tfTotalPayment, 1 , 1 ); 74 75 // Set text field alignment 76 tfFormattedInterestRate.setAlignment(Pos.BASELINE_RIGHT); 77 tfFormattedNumberOfYears.setAlignment(Pos.BASELINE_RIGHT); 78 tfFormattedLoanAmount.setAlignment(Pos.BASELINE_RIGHT); 79 tfTotalPayment.setAlignment(Pos.BASELINE_RIGHT); 80 tfMonthlyPayment.setAlignment(Pos.BASELINE_RIGHT); 81 82 // Set editable false 83 tfFormattedInterestRate.setEditable( false );

36-20 Chapter 36 Internationalization

84 tfFormattedNumberOfYears.setEditable( false ); 85 tfFormattedLoanAmount.setEditable( false ); 86 tfTotalPayment.setEditable( false ); 87 tfMonthlyPayment.setEditable( false ); 88 89 VBox vBox = new VBox( 5 ); 90 vBox.getChildren().addAll(hBox, 91 new Label( "Enter Annual Interest Rate, " + 92 "Number of Years, and Loan Amount" ), gridPane, 93 new Label( "Payment" ), gridPaneOutput, btCompute); 94 95 // Create a scene and place it in the stage 96 Scene scene = new Scene(vBox, 400 , 300 ); 97 primaryStage.setTitle( "NumberFormatDemo" ); // Set the stage title 98 primaryStage.setScene(scene); // Place the scene in the stage 99 primaryStage.show(); // Display the stage 100 101 // Register listeners 102 cboLocale.setOnAction(e -> { 103 locale = locales[cboLocale 104 .getSelectionModel().getSelectedIndex()]; 105 computeLoan(); 106 }); 107 108 btCompute.setOnAction(e -> computeLoan()); 109 } 110 111 /** Compute payments and display results locale-sensitive format */ 112 private void computeLoan() { 113 // Retrieve input from user 114 double loan = new Double(tfLoanAmount.getText()).doubleValue(); 115 double interestRate = 116 new Double(tfInterestRate.getText()).doubleValue() / 1240 ; 117 int numberOfYears = 118 new Integer(tfNumberOfYears.getText()).intValue(); 119 120 // Calculate payments 121 double monthlyPayment = loan * interestRate/ 122 ( 1 - (Math.pow( 1 / ( 1 + interestRate), numberOfYears * 12 ))); 123 double totalPayment = monthlyPayment * numberOfYears * 12 ; 124 125 // Get formatters 126 NumberFormat percentFormatter = 127 NumberFormat.getPercentInstance(locale); 128 NumberFormat currencyForm = 129 NumberFormat.getCurrencyInstance(locale); 130 NumberFormat numberForm = NumberFormat.getNumberInstance(locale); 131 percentFormatter.setMinimumFractionDigits( 2 ); 132 133 // Display formatted input 134 tfFormattedInterestRate.setText( 135 percentFormatter.format(interestRate * 12 )); 136 tfFormattedNumberOfYears.setText 137 (numberForm.format(numberOfYears)); 138 tfFormattedLoanAmount.setText(currencyForm.format(loan)); 139 140 // Display results in currency format 141 tfMonthlyPayment.setText(currencyForm.format(monthlyPayment)); 142 tfTotalPayment.setText(currencyForm.format(totalPayment)); 143 } 144 }