For example I need 5.0 to become 5, or 4.3000 to become 4.3.
-
11Note: 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.tommoyang– tommoyang2014-06-04 02:07:56 +00:00Commented Jun 4, 2014 at 2:07
-
2This is ugly as hell but it works: String.format(doubleVal).replaceAll("\\.0+$", "");mvmn– mvmn2019-01-18 15:55:07 +00:00Commented Jan 18, 2019 at 15:55
-
4P.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).mvmn– mvmn2019-01-20 12:38:34 +00:00Commented Jan 20, 2019 at 12:38
Add a comment
|
3 Answers
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
10 Comments
Peter Elliott
this fails the case of
5.0 becoming 5, as it will always add a .0 to the end of numbers.Marcin Szymczak
Thank you for Your comment. Now I have figured out that I should have used 0.#.
David Conrad
this fails in the case of
4.32 which becomes 4.3David Conrad
@Chisko The question asks how to "remove trailing zeros".
Rok Povsic
A double type can't really do this. If you're willing to switch to BigDecimal, here's the solution.
|
Use DecimalFormat
double answer = 5.0;
DecimalFormat df = new DecimalFormat("###.#");
System.out.println(df.format(answer));
2 Comments
VikrantY
Won't this round the numnber like in case of 5.000001
Mostafa Zeinali
It does and that's why this is wrong...
Use a DecimalFormat object with a format string of "0.#".
6 Comments
narancs
this fails in the case of 4.32 which becomes 4.3
dashrb
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
@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));
Peter Schorn
@dashrb The OP was very explicit about what he wanted. He wants to remove trailing zeroes. Do you understand what the word "trailing" means?
Gutter Overflow FKuAll
@UncleIroh FYI this provides correct solution even after removing the RoundingMode.HALF_UP but keeping #.###
|