dcsimg
 

Java: Working with Local Currencies

Monday May 17th 2021 by MS Sridhar
Article Image

This short Java tutorial talks about working with local currencies. Learn more.

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
Home
Mobile Site | Full Site