Search code examples
javadoublebigdecimaldecimalformat

DecimalFormat - Format decimal to preserve variable amount of trailing zeros


I have a situation where I need to preserve the number of decimal places for a number when formatting to a String using DecimalFormat, specifically for trailing zeros. I need to use DecimalFormat because I also need to avoid scientific notation for large numbers, and as seen here, this is the best way to do so. However, as you can see in that post, the code provided also removes trailing zeros, whereas I would like to preserve them.

1.00 -> "1.00"
1.000 -> "1.000"

I've seen a lot of questions that involve a fixed number of decimal places, but in my situation I need to account for a variable amount of decimal places. I looked into counting the number of decimal places, but since my input can also be a Double (in addition to BigDecimal), there appears to be no reliable way to count the digits after the decimal point for all numbers. Double.toString() does not work since Doubles break into exponential notation for very small and very big numbers. See here for more info regarding why it is difficult to count the number of decimal places in a double.


Solution

  • "Preserve" trailing zeros?

    A double value doesn't know how many trailing zeroes you want to see. It is just a number, and 1.00 and 1.000 are the same number, i.e. the number 1. What you are asking cannot be done with a double value.

    Now, BigDecimal does remember the number of trailing zeroes, so if you want to print a BigDecimal value, retaining the scale of the number, but ensuring it never prints in scientific notation, don't use a DecimalFormat, but instead use toPlainString():

    Returns a string representation of this BigDecimal without an exponent field.


    UPDATE

    If you want to print a double value with as many decimal fraction digits as needed (i.e. no trailing zeroes), and want to make sure it never prints in scientific notation, use a DecimalFormat with very high MaximumIntegerDigits and very high setMaximumFractionDigits.

    "Very high" means values exceeding the range of a double, so 999 is a good "round" number, though 330 would be high enough.

    Test

    DecimalFormat fmt = new DecimalFormat("0");
    fmt.setMaximumIntegerDigits(330);
    fmt.setMaximumFractionDigits(330);
    
    System.out.println("0.0123400  = " + 0.0123400               + "   = " + fmt.format(0.0123400));
    System.out.println("123400.00  = " + 123400.00                + "  = " + fmt.format(123400.00));
    System.out.println("NaN        = " + Double.NaN          + "       = " + fmt.format(Double.NaN));
    System.out.println("-INFINITY  = " + Double.NEGATIVE_INFINITY  + " = " + fmt.format(Double.NEGATIVE_INFINITY));
    System.out.println("+INFINITY  = " + Double.POSITIVE_INFINITY + "  = " + fmt.format(Double.POSITIVE_INFINITY));
    System.out.println("MIN_NORMAL = " + Double.MIN_NORMAL               + " = " + fmt.format(Double.MIN_NORMAL));
    System.out.println("MIN_VALUE  = " + Double.MIN_VALUE + "                = " + fmt.format(Double.MIN_VALUE));
    System.out.println("MAX_VALUE  = " + Double.MAX_VALUE               + "  = " + fmt.format(Double.MAX_VALUE));
    

    Output

    0.0123400  = 0.01234   = 0.01234
    123400.00  = 123400.0  = 123400
    NaN        = NaN       = �
    -INFINITY  = -Infinity = -∞
    +INFINITY  = Infinity  = ∞
    MIN_NORMAL = 2.2250738585072014E-308 = 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022250738585072014
    MIN_VALUE  = 4.9E-324                = 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000049
    MAX_VALUE  = 1.7976931348623157E308  = 179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000