From: Fredrik Roubert Date: Thu, 16 Aug 2018 00:54:32 +0000 (+0200) Subject: ICU-20000 Workaround for BigDecimal.stripTrailingZeros() differences. (#57) X-Git-Tag: release-63-rc~115 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=da7bd533ab75076b14ee55d8f8631fc6f882505c;p=icu ICU-20000 Workaround for BigDecimal.stripTrailingZeros() differences. (#57) Different implementations of BigDecimal.stripTrailingZeros(), in different versions of the JDK (and different versions of Android), have differences in their handling of zero. To avoid this, ICU4J can return BigDecimal.ZERO for any value that is equal to zero, instead of calling BigDecimal.stripTrailingZeros() in this problematic case. --- diff --git a/icu4j/main/classes/core/src/com/ibm/icu/number/Scale.java b/icu4j/main/classes/core/src/com/ibm/icu/number/Scale.java index ef3e3868fda..cdcf8259c64 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/number/Scale.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/number/Scale.java @@ -40,7 +40,11 @@ public class Scale { private Scale(int magnitude, BigDecimal arbitrary, MathContext mc) { if (arbitrary != null) { // Attempt to convert the BigDecimal to a magnitude multiplier. - arbitrary = arbitrary.stripTrailingZeros(); + // ICU-20000: JDKs have inconsistent behavior on stripTrailingZeros() for Zero. + arbitrary = + arbitrary.compareTo(BigDecimal.ZERO) == 0 + ? BigDecimal.ZERO + : arbitrary.stripTrailingZeros(); if (arbitrary.precision() == 1 && arbitrary.unscaledValue().equals(BigInteger.ONE)) { // Success! magnitude -= arbitrary.scale();