Java NumberFormat set currency

Description

Java NumberFormat set currency

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);

      /*from   w w  w  .ja  va 2s  .c  om*/
      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