Java Data Type How to - Format a Number with Constant Locale Usage








Question

We would like to know how to format a Number with Constant Locale Usage.

Answer

import java.text.NumberFormat;
import java.util.Locale;
/*from w ww.  j  av  a 2s .  c o m*/
public class Main {

  public static void main(String[] argv) {
    NumberFormat numberFormat = NumberFormat.getInstance();
    numberFormat.setParseIntegerOnly(false);
    double usersNumber = 197912.29;

    numberFormat = NumberFormat.getNumberInstance(Locale.US);
    System.out.println("User's number (US): "
        + numberFormat.format(usersNumber));
    numberFormat = NumberFormat.getNumberInstance(Locale.GERMANY);
    System.out.println("User's number (GERMANY): "
        + numberFormat.format(usersNumber));
    numberFormat = NumberFormat.getNumberInstance();
    System.out.println("User's number (DEFAULT LOCALE): "
        + numberFormat.format(usersNumber));
  }
}

The code above generates the following result.