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?

- 53,877
- 76
- 193
- 251
-
3A double cannot represent an irrational value... – Oliver Charlesworth Jul 01 '12 at 19:23
-
2A 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. – Joop Eggen Jul 01 '12 at 19:23
-
@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. – Mohit Deshpande Jul 01 '12 at 19:25
-
Yes, and BTW a DecimalFormat does not modify the original value. – Joop Eggen Jul 01 '12 at 19:26
-
Possible duplicate of http://stackoverflow.com/questions/703396/how-to-nicely-format-floating-types-to-string – Vadzim Aug 31 '12 at 12:59
4 Answers
If you are willing to switch to BigDecimal
, there is a #stripTrailingZeroes() method that accomplishes this.

- 45,603
- 8
- 97
- 119
-
-
3But wouldn't that put a number like 600.0 in scientific notation? – Mohit Deshpande Jul 01 '12 at 19:29
-
6@Jesse Rusak: Yes. @Mohit: Not if you use `#toPlainString()` for output. – Keppil Jul 01 '12 at 19:31
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));

- 1,499
- 10
- 14
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)

- 61
- 1
- 2
-
1Please edit the code blocks according to [help document](http://stackoverflow.com/editing-help#code). In most cases, you don't actually need HTML tags. – Be Brave Be Like Ukraine Dec 21 '12 at 20:05
double result = ...;
int intResult = (int)result;
if (result == intResult) {
System.out.println(intResult);
} else {
System.out.println(result);
}