How do I display the currency for a locale?
Author: Deron Eriksson
Description: This Java tutorial describes how to display currency information about a locale.
Tutorial created using:
Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)
A Currency object can be obtained for a particular locale via a call to Currency.getInstance(locale). The CurrencyTest class demonstrates getting a Currency object for a locale. Once a Currency object is obtained, it displays the currency code, symbol, and the default fraction digits for that currency. The CurrencyTest class does this for the default locale and a Swedish locale. CurrencyTest.javapackage test; import java.util.Currency; import java.util.Locale; public class CurrencyTest { public static void main(String[] args) throws Exception { Locale defaultLocale = Locale.getDefault(); displayCurrencyInfoForLocale(defaultLocale); Locale swedishLocale = new Locale("sv", "SE"); displayCurrencyInfoForLocale(swedishLocale); } public static void displayCurrencyInfoForLocale(Locale locale) { System.out.println("Locale: " + locale.getDisplayName()); Currency currency = Currency.getInstance(locale); System.out.println("Currency Code: " + currency.getCurrencyCode()); System.out.println("Symbol: " + currency.getSymbol()); System.out.println("Default Fraction Digits: " + currency.getDefaultFractionDigits()); System.out.println(); } } The output of CurrencyTest is shown below. Locale: English (United States) Currency Code: USD Symbol: $ Default Fraction Digits: 2 Locale: Swedish (Sweden) Currency Code: SEK Symbol: SEK Default Fraction Digits: 2 Related Tutorials:
|