First Thanks to Mr. Wayan Saryada for advice to Change Symbol of Currency Format.
In this code snippet you will learn how to change Currency Symbol Locale by DecimalFormat
. First Of All we need create an instance of object Locale (we use US Locale). Then make e object DecimalFormat
using static method getCurrencyInstance
. DecimalFormatSymbols
is a class represents the set of symbols (such as the decimal separator, the grouping separator, and so on) needed by DecimalFormat
to format numbers. After all apply object decimal format number to object decimal format by using method setCurrencySymbol
.
Let’s see the code snippet below as an example:
import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.util.Locale; public class ChangeSymbolCurrencyLocale { public static void main(String[] args) { Locale locale = Locale.US; DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getCurrencyInstance(locale); DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(locale); dfs.setCurrencySymbol("USD "); decimalFormat.setDecimalFormatSymbols(dfs); BigDecimal money=BigDecimal.valueOf(10000); // Print Money With Default Setting Locale US; System.out.println("Money Format\t\t\t: " + DecimalFormat.getCurrencyInstance(Locale.US).format(money)); // Print Money With Change Sysmbol Currency System.out.println("Money Format Change Symbol\t: "+decimalFormat.format(money)); } }
The Result
Money Format : $10,000.00 Money Format Change Symbol : USD 10,000.00