Java - format number with comma

Print an integer in Java with commas as thousands separators. For example,  the number 1234567 as "1,234,567"

Code:

public static String formatNumberWithComma(String number) {

  double amount = Double.parseDouble(number);

  DecimalFormat formatter = new DecimalFormat("#,###");

  return formatter.format(amount);

 }

Call this function:
System.out.println(formatNumberWithComma("1102345"));
Result: 1,102,345

Leave a Reply

Powered by Blogger.