Overriding the Default Currency format - Java Internationalization

Java examples for Internationalization:Locale

Description

Overriding the Default Currency format

Demo Code

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

    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);
  }/*ww w .  j a  v  a2  s . co  m*/
}

Result


Related Tutorials