82

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

3
  • 11
    Note: Unlike the top answer in the linked question, all of these answers rely on an expected input of only one decimal place. Everything longer than one decimal place will be rounded, which may not be desired behaviour. Commented Jun 4, 2014 at 2:07
  • 2
    This is ugly as hell but it works: String.format(doubleVal).replaceAll("\\.0+$", ""); Commented Jan 18, 2019 at 15:55
  • 4
    P.S. Ended up using this: DecimalFormat("#.################").format(doubleVal); // This ensures no trailing zeroes and no separator if fraction part is 0 (there's a special method setDecimalSeparatorAlwaysShown(false) for that, but it seems to be already disabled by default). But will produce up to 16 digits of fractional part (and you can put more # there if you think 16 is not enough). Commented Jan 20, 2019 at 12:38

3 Answers 3

109

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
Sign up to request clarification or add additional context in comments.

10 Comments

this fails the case of 5.0 becoming 5, as it will always add a .0 to the end of numbers.
Thank you for Your comment. Now I have figured out that I should have used 0.#.
this fails in the case of 4.32 which becomes 4.3
@Chisko The question asks how to "remove trailing zeros".
A double type can't really do this. If you're willing to switch to BigDecimal, here's the solution.
|
9

Use DecimalFormat

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

2 Comments

Won't this round the numnber like in case of 5.000001
It does and that's why this is wrong...
1

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

6 Comments

this fails in the case of 4.32 which becomes 4.3
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).
@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));
@dashrb The OP was very explicit about what he wanted. He wants to remove trailing zeroes. Do you understand what the word "trailing" means?
@UncleIroh FYI this provides correct solution even after removing the RoundingMode.HALF_UP but keeping #.###
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.