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.
This commit is contained in:
Fredrik Roubert 2018-08-16 02:54:32 +02:00 committed by Shane Carr
parent 01ca8fb555
commit da7bd533ab
No known key found for this signature in database
GPG key ID: FCED3B24AAB18B5C

View 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();