Convert Number in Locale Format


import java.util.Locale;
import java.text.NumberFormat;
import java.text.ParseException;
public class NumberLocalizationExample{
@SuppressWarnings("static-access")
public static void main(String[] args) {
Locale locale = Locale.CANADA;
String string = 
NumberFormat.getNumberInstance(locale).format(-1234.56); 
System.out.println("CANADA Format: "+ string);

// Format for GERMAN locale
locale = Locale.GERMAN;
string = NumberFormat.getNumberInstance(
locale).format(-1234.56);
System.out.println("GERMAN Format: "+string);

// Format for the default locale
string = NumberFormat.getNumberInstance().format(-1234.56);
System.out.println("Default Format: "+string);

try {
Number number = NumberFormat.getNumberInstance(
locale.JAPAN).parse("-1.234,56");

if (number instanceof Long) {
System.out.println("Converted Number In Long: "+
number.toString());
} else if (number instanceof Double) {
System.out.println("Converted Number In Double: "+
number.toString());
}
} catch (ParseException e) {
e.printStackTrace();
}

}
}


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email