Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Server-Side Web Programming: Formatting and Internationalization

This document discusses formatting numbers and dates for different locales to support internationalization. It explains how to use NumberFormat and DateFormat objects to format numbers and dates according to the conventions of a given locale. These objects allow setting the number of decimal places, currency symbols, and date formats appropriately for the language and country of the user. The locale is obtained from the request and passed to the format methods to ensure the output is tailored to that specific locale.

Uploaded by

Hop Huynh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Server-Side Web Programming: Formatting and Internationalization

This document discusses formatting numbers and dates for different locales to support internationalization. It explains how to use NumberFormat and DateFormat objects to format numbers and dates according to the conventions of a given locale. These objects allow setting the number of decimal places, currency symbols, and date formats appropriately for the language and country of the user. The locale is obtained from the request and passed to the format methods to ensure the output is tailored to that specific locale.

Uploaded by

Hop Huynh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

Server-side

Web Programming
Lecture 10:
Formatting and
Internationalization
Formatting Numbers
• Goal: More readable numeric output
– Specific numbers of digits after decimal place, etc.
Formatting Numbers
• NumberFormat object
– Object that sets Strings in some desired format
– In java.text.* package

• Get a new instance


NumberFormat nf = NumberFormat.getInstance();

• Set properties of that object


nf.methodsThatSetProperties(parameters);

• Use that object to format numeric strings for output


<%= nf.format(some string) %>
Formatting Numbers
• Example:
nf.setMinimumFractionDigits(n) can be used to set max,
nf.setMaximumFractionDigits(n) min digits after decimal

Always force exactly


2 digits to be shown
Formatting Numbers
• Can use to set strings in output
Format Types
• Java has formats for different types of numbers:
– Currency: Automatically 2 digits, inserts $ in front
6.3  $6.30
– Percent: Automatically multiplies by 100, adds % to end
0.67  67%
• Syntax:
NumberFormat nf = NumberFormat.getFormatInstance();
Format Types
• Use format to apply to a string as before
Internationalization
• Formats use different conventions in different parts of the world!
• Your applications must adjust to this!

United States Great Britain French Canada


Locales
• Every computer has a locale
– Set when computer shipped/set up
– May be accessed using Control Panel

• Locale passed with request


Locale here = request.getLocale();
• Pass to get____Instance methods to get proper form for locale
of this customer
• format uses correct format for the given locale
Dates and Internationalization
• Formats of dates also locale specific

• Create DateFormat object for locale


DateFormat df =
DateFormat.getDateFormat(DateFormat.style, here);
• FULL style: Wednesday, October 8, 2008
• LONG style: October 8, 2008
• MEDIUM style: Oct 8, 2008
• SHORT style: 10/8/08

• Apply format to new calendar object to show current date in correct


format
Dates and Internationalization
Locale Information
• Can extract country and language of locale
– Country = 2 character abbreviation (“US”, “CA”, etc.)
– Language = ISO-639 language code (“en”, “fr”, “zh”, etc.)

• Locale here = request.getLocale();


String lang = here.getLanguage();
String country = here.getCountry();

• Can use to make decisions about what to display


– More likely, which text files to forward to or include
Locale Information

You might also like