Java Data Type How to - Parse number with NumberFormat and Locale








Question

We would like to know how to parse number with NumberFormat and Locale.

Answer

   /*from   ww  w.  ja v  a  2  s . c o m*/
import java.text.NumberFormat;
import java.util.Locale;

public class Main {
  public static void main(String[] argv) throws Exception {

    Number number = NumberFormat.getCurrencyInstance(Locale.GERMANY).parse("$123.45");
    if (number instanceof Long) {
      System.out.println("Long value");
    } else {
      System.out.println("Double value");
    }
  }
}

The code above generates the following result.