dcsimg
LinkedIn
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
May 17, 2021

Java: Working with Local Currencies

In Java, we can work with local currencies by using Java's built-in support for currency, called java.util.Locale. Here is an example program and code snippet showing how to use the local currency library.

*/

import java.text.NumberFormat;  
import java.util.Locale;  

public class LocaleBasedCurrency {

    public static void main(String[] args)
	{
		LocaleBasedCurrency localeBasedCurrency = new LocaleBasedCurrency();
		localeBasedCurrency.proceed(Locale.US);
		localeBasedCurrency.proceed(Locale.UK);
	}

	private void proceed(Locale localeArg)
	{
		double doubleValue = 1400.65;  
		NumberFormat numberFormat = NumberFormat.getCurrencyInstance(localeArg);  
		String localBasedcurrency = numberFormat.format(doubleValue);  
		System.out.println("Locale: " + localeArg + ", Currency: " + localBasedcurrency);
	}  
  
} 

/*

Output:

$java LocaleBasedCurrency
Locale: en_US, Currency: $1,400.65
Locale: en_GB, Currency: £1,400.65
MS Sridhar
 
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date