Every country has its own unique way of expressing currency. Hence, expressing the currency in a proper format becomes crucial when dealing with currencies. Manually formatting currency for every country is a tedious process.
Java provides an automated way for formatting currencies depending on the locale.
getCurrencyInstance() is a static method of the NumberFormat class that returns the currency format for the specified locale.
Note: A
Localein Java represents a specific geographical, political, or cultural region.
public static NumberFormat getCurrencyInstance(Locale inLocale)
Locale inLocale: The desired locale.The method returns a NumberFormat instance for the given locale.
import java.math.BigDecimal;import java.text.NumberFormat;import java.util.Locale;public class Main {public static void main(String[] args){BigDecimal valueToFormat = new BigDecimal("432.6542");NumberFormat franceFormat = NumberFormat.getCurrencyInstance(Locale.FRANCE);String formattedString = franceFormat.format(valueToFormat);System.out.println(formattedString);}}
BigDecimal value to format called valueToFormat.getCurrencyInstance() method. The FRANCE locale is passed as an argument to the method. We name the obtained format as franceFormat.format() method on franceFormat. valueToFormat is passed as an argument to the method.