ICU-13156 Fixing ArithmeticException when formatting small numbers with CompactDecimalFormat.

X-SVN-Rev: 40066
This commit is contained in:
Shane Carr 2017-04-19 21:26:44 +00:00
parent a82e70e5b1
commit 33b124a95d
4 changed files with 17 additions and 2 deletions

View file

@ -204,11 +204,14 @@ public abstract class Rounder extends Format.BeforeFormat {
// TODO: Avoid the object creation here.
FormatQuantity copy = input.createCopy();
assert !input.isZero();
int magnitude = input.getMagnitude();
int multiplier = mg.getMultiplier(magnitude);
input.adjustMagnitude(multiplier);
apply(input);
if (input.getMagnitude() == magnitude + multiplier + 1) {
// If the number turned to zero when rounding, do not re-attempt the rounding.
if (!input.isZero() && input.getMagnitude() == magnitude + multiplier + 1) {
magnitude += 1;
input.copyFrom(copy);
multiplier = mg.getMultiplier(magnitude);

View file

@ -221,8 +221,10 @@ public class CompactDecimalFormat extends Format.BeforeFormat {
magnitude = 0;
rounder.apply(input);
} else {
// TODO: Revisit chooseMultiplierAndApply
int multiplier = rounder.chooseMultiplierAndApply(input, data);
magnitude = input.getMagnitude() - multiplier;
magnitude = input.isZero() ? 0 : input.getMagnitude();
magnitude -= multiplier;
}
StandardPlural plural = input.getStandardPlural(rules);

View file

@ -188,6 +188,7 @@ public class ScientificFormat extends Format.BeforeFormat implements Rounder.Mul
rounder.apply(input);
exponent = 0;
} else {
// TODO: Revisit chooseMultiplierAndApply
exponent = -rounder.chooseMultiplierAndApply(input, this);
}

View file

@ -750,4 +750,13 @@ public class CompactDecimalFormatTest extends TestFmwk {
String s = cdf.format(-1500);
assertEquals("Should work with negative numbers", "-1.5K", s);
}
@Test
public void TestBug13156() {
ULocale loc = ULocale.ENGLISH;
CompactDecimalFormat cdf = CompactDecimalFormat.getInstance(loc, CompactStyle.SHORT);
cdf.setMaximumFractionDigits(1);
String result = cdf.format(0.01);
assertEquals("Should not throw exception on small number", "0", result);
}
}