Remove trailing zeros from double
Asked Answered
M

4

10

I would like to remove all trailing zeros without truncating or rounding the number if it doesn't have any. For example, the number could be something like 12.0, in which case, the trailing zero should be removed. But the number could also be something almost irrational, like 12.9845927346958762... going on an on to the edge of the screen. Is there a way to setup DecimalFormat or some other class to cut of trailing zeros, while keeping the irrationality intact?

Mona answered 1/7, 2012 at 19:17 Comment(5)
A double cannot represent an irrational value...Phthalein
A DecimalFormat with 0.### will do such a thing. Of course double is just an approximation of floating point numbers. Maybe use BigDecimal, but that also is a final class.Remove
@JoopEggen the zero before the decimal point would allow for more than the units place, such as something like 234123412.1? Sorry, I have never used DecimalFormat before.Mona
Yes, and BTW a DecimalFormat does not modify the original value.Remove
Possible duplicate of #703896Aureomycin
C
26

If you are willing to switch to BigDecimal, there is a #stripTrailingZeroes() method that accomplishes this.

Clothesbasket answered 1/7, 2012 at 19:27 Comment(3)
Does that affect the resulting string representation?Rillet
But wouldn't that put a number like 600.0 in scientific notation?Mona
@Jesse Rusak: Yes. @Mohit: Not if you use #toPlainString() for output.Clothesbasket
E
6

You can use String manipulation to remove trailing zeros.

private static String removeTrailingZeros(double d) {
  return String.valueOf(d).replaceAll("[0]*$", "").replaceAll(".$", "");
}

System.out.println(removeTrailingZeros(1234.23432400000));
System.out.println(removeTrailingZeros(12.0));
Evite answered 1/7, 2012 at 19:52 Comment(1)
I think last "replace" should be more like: .replaceAll("\\.$", "");Wunder
B
6

private static String removeTrailingZeros(double d) {
return String.valueOf(d).replaceAll("[0]*$", "").replaceAll(".$", "");
}

or

private static String removeTrailingZeros(double d) {
return String.valueOf(d).replaceAll(".?0*$", "");
}

These are wrong code.

System.out.println(removeTrailingZeros(1234.23432400000));
return: 1234.23432
but must return: 1234.234324


private static String removeTrailingZeros(double myDouble) {
return (new BigDecimal(Double.toString(myDouble))).toPlainString().replaceAll("[0]
*$", "").replaceAll(".$", "");
}

This method is working wrong too

System.out.println(removeTrailingZeros(472.304000)); returns 472.30 instead of 472.304

System.out.println(removeTrailingZeros(472304000)); returns 47230 instead of 472304000


The #stripTrailingZeroes() or toPlainString() of the BigDecimal are good method, but nor alone.

//----------------BigDecimal.toPlainString-------------
System.out.println((new BigDecimal(Double.toString(4724))).toPlainString());
System.out.println((new BigDecimal(Double.toString(472304000))).toPlainString());

returns:
4724.0 (this is not fine - we don't want '.0')
472304000 (This is fine)

//----------------BigDecimal.stripTrailingZeros-------------
System.out.println((new BigDecimal(Double.toString(4724)).stripTrailingZeros()));
System.out.println((new BigDecimal(Double.toString(472304000d)).stripTrailingZeros()));

returns:
4724.0 (This is fine)
4.72304E+8 (This is not fine - we want 472304000)

The perfect resolution of the currect subject "Remove trailing zeros from double" is using
.stripTrailingZeros().toPlainString()

For example :
//---------BigDecimal.stripTrailingZeros.toPlainString-----------------
System.out.println(new BigDecimal(Double.toString(472.304000)).stripTrailingZeros().toPlainString());
System.out.println((new BigDecimal(Double.toString(4724)).stripTrailingZeros().toPlainString()));
System.out.println((new BigDecimal(Double.toString(472304000d)).stripTrailingZeros().toPlainString()));

Result is:
472.304 (correct)
4724 (correct)
472304000 (correct)

Bicentennial answered 21/12, 2012 at 19:44 Comment(1)
Please edit the code blocks according to help document. In most cases, you don't actually need HTML tags.Looper
A
-3
double result = ...;
int intResult = (int)result;
if (result == intResult) {
    System.out.println(intResult);
} else {
    System.out.println(result);
}
Ancillary answered 19/2, 2016 at 0:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.