- Given Locale for given language & country code in java.
- We would like to display currency code & symbol for given locale.
1. Example: Display currency code/ symbol from Locale in java
- Suppose we would like to display currency code for country=US and Language=English.
- We would use default locale class to get currency information.
- We should get output as “Currency Code=USD, Symbol=$” for given locale.
2. Program: display currency code/ symbol from Locale in java (example)
package org.learn; import java.util.Currency; import java.util.Locale; public class GetCurrencyByLocale { public static void main(String[] args) { //Display properties for language = english & Country = USA displayCurrency( "en" , "US" ); //Display properties for language = french & Country = france displayCurrency( "fr" , "FR" ); //Display properties for language = hindi & Country = india displayCurrency( "hi" , "IN" ); //Display properties for language = russian & Country = russia displayCurrency( "ru" , "RU" ); } private static void displayCurrency(String languageCode, String countryCode) { Locale locale = new Locale(languageCode, countryCode); Currency currency = Currency.getInstance(locale); String code = currency.getCurrencyCode(); String symbol = currency.getSymbol(); System.out.printf( "Loading currency info for CountryCode=%s,LanguageCode=%s" , countryCode, languageCode); System.out.println(); System.out.printf( "Currency Code=%s, Symbol=%s" ,code, symbol ); System.out.println(); System.out.println(); } } |
3. Output: display currency code/ symbol from Locale in java (example)
Loading currency info for CountryCode=US,LanguageCode=en Currency Code=USD, Symbol=$ Loading currency info for CountryCode=FR,LanguageCode=fr Currency Code=EUR, Symbol=EUR Loading currency info for CountryCode=IN,LanguageCode=hi Currency Code=INR, Symbol=INR Loading currency info for CountryCode=RU,LanguageCode=ru Currency Code=RUB, Symbol=RUB |