Format currency in your US locale in Java
Description
The following code shows how to format currency in your US locale.
Example
// w w w. j a v a2 s. c o m
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Currency;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
BigDecimal value = new BigDecimal(12345);
Locale.setDefault(Locale.US);
NumberFormat nf = NumberFormat.getCurrencyInstance();
String formattedCurrency = nf.format(value);
System.out.printf("%s\n", formattedCurrency);
nf.setCurrency(Currency.getInstance("JPY"));
formattedCurrency = nf.format(value);
System.out.printf("%s\n\n", formattedCurrency);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »