How to remove trailing zeros from a double

126,056

Solution 1

Use DecimalFormat

  double answer = 5.0;
   DecimalFormat df = new DecimalFormat("###.#");
  System.out.println(df.format(answer));

Solution 2

You should use DecimalFormat("0.#")


For 4.3000

Double price = 4.3000;
DecimalFormat format = new DecimalFormat("0.#");
System.out.println(format.format(price));

output is:

4.3

In case of 5.000 we have

Double price = 5.000;
DecimalFormat format = new DecimalFormat("0.#");
System.out.println(format.format(price));

And the output is:

5

Solution 3

Use a DecimalFormat object with a format string of "0.#".

Share:
126,056
Wrath
Author by

Wrath

Updated on December 31, 2021

Comments

  • Wrath
    Wrath over 2 years

    For example I need 5.0 to become 5, or 4.3000 to become 4.3.

  • Peter Elliott
    Peter Elliott over 11 years
    this fails the case of 5.0 becoming 5, as it will always add a .0 to the end of numbers.
  • Marcin Szymczak
    Marcin Szymczak over 11 years
    Thank you for Your comment. Now I have figured out that I should have used 0.#.
  • David Conrad
    David Conrad over 10 years
    this fails in the case of 4.32 which becomes 4.3
  • VikrantY
    VikrantY almost 10 years
    Won't this round the numnber like in case of 5.000001
  • Mostafa Zeinali
    Mostafa Zeinali about 9 years
    It does and that's why this is wrong...
  • narancs
    narancs about 8 years
    this fails in the case of 4.32 which becomes 4.3
  • dashrb
    dashrb about 8 years
    fair point, although the OP didn't indicate how many places were desired. I can't think of a good reason to truncate trailing 0's but keep non-zeros as far as they are printable. IMHO, when printing a double, the code should specify the desired number of digits of precision (the number of #'s after the period).
  • Uncle Iroh
    Uncle Iroh almost 8 years
    @Karoly DecimalFormat df = new DecimalFormat("###.##"); df.setRoundingMode(RoundingMode.HALF_UP); System.err.println(df.format(4.325)); System.err.println(df.format(4.30)); System.err.println(df.format(4.00));
  • Chisko
    Chisko over 7 years
    @DavidConrad yes, because it is specifying only one decimal digit precision
  • David Conrad
    David Conrad over 7 years
    @Chisko The question asks how to "remove trailing zeros".
  • Chisko
    Chisko over 7 years
    But the description clarifies
  • Mohit Rajput
    Mohit Rajput over 5 years
    It fails, 4.300001 becomes 4.3 and if you want to convert it to double, it will add .0 again.
  • JU5T1C3
    JU5T1C3 over 5 years
    be aware that the output is 4,3 and not 4.3
  • Rok Povsic
    Rok Povsic about 5 years
    A double type can't really do this. If you're willing to switch to BigDecimal, here's the solution.
  • fHate
    fHate almost 4 years
    But how to be with values like 0.0005000 (must shown as 0.0005)?
  • Peter Schorn
    Peter Schorn over 3 years
    @dashrb The OP was very explicit about what he wanted. He wants to remove trailing zeroes. Do you understand what the word "trailing" means?
  • Prabhat Gaur
    Prabhat Gaur over 3 years
    @UncleIroh FYI this provides correct solution even after removing the RoundingMode.HALF_UP but keeping #.###