[Java] String with number format

Reference

public static void main(String[] args) throws Exception {
    String number = "1000500000";
    double amount = Double.parseDouble(number);
    DecimalFormat intFormatter = new DecimalFormat("#,###");
    DecimalFormat floatFormatter = new DecimalFormat("#,###.00");

    System.out.println("==int==");
    System.out.println(intFormatter.format(amount));
    System.out.println(String.format("%,d", Integer.parseInt((number))));
    System.out.println(number.replaceAll("(\\d)(?=(\\d{3})+$)", "$1,"));

    System.out.println("==float==");
    System.out.println(floatFormatter.format(amount));
    System.out.println(String.format("%,.2f", Float.parseFloat((number))));
}


==int==
1,000,500,000
1,000,500,000
1,000,500,000
==float==
1,000,500,000.00
1,000,499,968.00
This entry was posted in Java. Bookmark the permalink.