Search code examples
javadoubledecimalformatdecimal-point

cant see two decimal value when number is in 10 series after formatting


I want to convert double value up to two decimals. It is working fine for values like 6779.77,22334.22 but it doesn't work for value 33445.90,3334.30 it shows only 33445.9 and 3334.3 values does not show 0.

Why is it so?

 public static String formatAmount(String number) {
    double amount = Double.parseDouble(number);
    DecimalFormat formatter = new DecimalFormat("#,##,##,###.##");

     return formatter.format(amount);
}

Solution

  • # means that trailing zeroes can be omitted. Instead, you should use the 0 format character:

    DecimalFormat formatter = new DecimalFormat("#,##,##,###.00");
    // Here -------------------------------------------------^