]> granicus.if.org Git - icu/commitdiff
ICU-20000 Workaround for BigDecimal.stripTrailingZeros() differences. (#57)
authorFredrik Roubert <fredrik@roubert.name>
Thu, 16 Aug 2018 00:54:32 +0000 (02:54 +0200)
committerShane Carr <shane@unicode.org>
Thu, 27 Sep 2018 21:27:38 +0000 (14:27 -0700)
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.

icu4j/main/classes/core/src/com/ibm/icu/number/Scale.java

index ef3e3868fda2174f9ddd4e6035c4dac6de3fad8d..cdcf8259c649f273eacbe685de322b0b32688fe3 100644 (file)
@@ -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();