Java Currency create by locale

Description

Java Currency create by locale

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.JAPAN);
      System.out.printf("Default locale: %s\n", Locale.getDefault().getDisplayName());
      NumberFormat nf = NumberFormat.getCurrencyInstance();
      String formattedCurrency = nf.format(value);
      System.out.printf("%s\n", formattedCurrency);
      nf.setCurrency(Currency.getInstance(Locale.US));
      formattedCurrency = nf.format(value);
      System.out.printf("%s\n\n", formattedCurrency);

      // w  ww . j  av a  2  s .c  o m
      Locale.setDefault(Locale.US);
      System.out.printf("Default locale: %s\n", Locale.getDefault().getDisplayName());
      nf = NumberFormat.getCurrencyInstance();
      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);

      Locale.setDefault(Locale.FRANCE);
      System.out.printf("Default locale: %s\n", Locale.getDefault().getDisplayName());
      nf = NumberFormat.getCurrencyInstance();
      formattedCurrency = nf.format(value);
      System.out.printf("%s\n", formattedCurrency);
      nf.setCurrency(Currency.getInstance("USD"));
      formattedCurrency = nf.format(value);
      System.out.printf("%s\n\n", formattedCurrency);

   }
}



PreviousNext

Related