Formatting an Integer to String in Java

There are times when you need to format a integer or double value into a String, with the comma for values larger than a thousand. If you need to format a numeric value correctly based on the local formatting rules for numbers use the NumberFormat class. Different locales have different formatting rules, for example in France they use the comma as a decimal point where in the United States the comma is used for delimiting large numbers.

double val = 123456.78;
System.out.println(val); // 123456.78

// Use default locale to format string
String localFormat = NumberFormat.getNumberInstance().format(val);
System.out.println(localFormat); // 123,456.78
		
String frenchFormat = NumberFormat.getNumberInstance(Locale.FRANCE).format(val);
System.out.println(frenchFormat); // 123 456,78

Leave a Reply