12

I have met strange bug in my code.

It relates with

new BigDecimal("1.2300").stripTrailingZeros() 

returns 1.23(correct)

but

new BigDecimal("0.0000").stripTrailingZeros() 

returns 0.0000(strange), thus nothing happens

Why?

How to fix it?

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

2 Answers2

18

Seems that this is a bug (JDK-6480539) which was fixed in Java 8 (per OpenJDK commit 2ee772cda1d6).

Workaround for earlier versions of Java:

BigDecimal zero = BigDecimal.ZERO;
if (someBigDecimal.compareTo(zero) == 0) {
    someBigDecimal = zero;
} else {
    someBigDecimal = someBigDecimal.stripTrailingZeros();
}
M. Justin
  • 14,487
  • 7
  • 91
  • 130
Yuri
  • 1,748
  • 2
  • 23
  • 26
  • Why downvoting? I've faced with similar issues on the older version of jdk. By the way in the answer is direct link to that bug. – Yuri Dec 22 '15 at 11:31
  • i cannot change jdk version in my project) Is there another way? – gstackoverflow Dec 22 '15 at 11:36
  • @gstackoverflow When you can't update your JDK version, then you need to perform the fix manually with something like this: `BigDecimal stripped = blub.intValue() == 0 ? BigDecimal.ZERO : blub.stripTrailingZeros();`. – Tom Dec 22 '15 at 11:39
  • 2
    @JordiCastilla OP is asking about unexpected behaviour of a standard library function, Yuri provides an answer with sources that it's a bug in the standard library. How is that not an answer? – Zoltán Dec 22 '15 at 11:40
  • 1
    @JordiCastilla [It would still state, that this is a (fixed) bug.](http://meta.stackexchange.com/questions/225370/your-answer-is-in-another-castle-when-is-an-answer-not-an-answer) – Tom Dec 22 '15 at 11:43
  • @Yuri great workaround, but please [read how to answer](http://stackoverflow.com/help/how-to-answer) in SO: ***Provide context for links:** Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline.* update it and you will get my upvote – Jordi Castilla Dec 22 '15 at 11:44
  • 5
    Don’t waste resources creating your own zero instance. Use [`BigDecimal.ZERO`](http://docs.oracle.com/javase/1.5.0/docs/api/java/math/BigDecimal.html#ZERO) – Holger Dec 22 '15 at 11:44
  • @Holger Actually that's a good point. Thanks for that, updated and included in the answer. – Yuri Dec 22 '15 at 11:52
  • 1
    You don’t need to structure your answer by putting “UPD #x” sections beneath each other. Those, who are interested in the history, can see it by clicking on the [“edited …” link](http://stackoverflow.com/posts/34414905/revisions). So don’t hesitate to replace `new BigDecimal("0")` by `BigDecimal.ZERO` in your original code. You don’t need to cite me. – Holger Dec 22 '15 at 12:42
  • Doesn't seem like it is fixed in Java 8. – Suhas Jan 13 '19 at 14:19
0

Here's the Javadoc for that method, which certainly suggests that isn't the intended behaviour, but isn't conclusive: http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#stripTrailingZeros()

Exactly why it isn't doing this is down to the implementation then. Which JDK are you using? For OpenJDK, we can see the source to figure out how it's reaching this conclusion, but other JDKs may differ.

hugh
  • 2,237
  • 1
  • 12
  • 25