]> granicus.if.org Git - icu/commitdiff
ICU-13808 Document ArithmeticException thrown by DecimalFormat
authorVictor Chang <vichang@google.com>
Fri, 24 Aug 2018 14:53:15 +0000 (15:53 +0100)
committerShane Carr <shane@unicode.org>
Thu, 27 Sep 2018 21:27:39 +0000 (14:27 -0700)
- Document the exception in the following methods
setMultiplier, setMathContext, setMathContextICU
- Add test to check the documented behavior

icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormat.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java

index 1185b5ba12648ea1226471aed01702050e1a65cd..ea2a1ca16e61084c0113ec05b4b29e61c7069543 100644 (file)
@@ -1123,6 +1123,8 @@ public class DecimalFormat extends NumberFormat {
    *
    * @param multiplier The number by which all numbers passed to {@link #format} will be multiplied.
    * @throws IllegalArgumentException If the given multiplier is zero.
+   * @throws ArithmeticException when inverting multiplier produces a non-terminating decimal result
+   *         in conjunction with MathContext of unlimited precision.
    * @category Multipliers
    * @stable ICU 2.0
    */
@@ -1296,6 +1298,8 @@ public class DecimalFormat extends NumberFormat {
    * method.
    *
    * @param mathContext The MathContext to use when rounding numbers.
+   * @throws ArithmeticException when inverting multiplier produces a non-terminating decimal result
+   *         in conjunction with MathContext of unlimited precision.
    * @see java.math.MathContext
    * @category Rounding
    * @stable ICU 4.2
@@ -1330,6 +1334,8 @@ public class DecimalFormat extends NumberFormat {
    * {@link com.ibm.icu.math.MathContext}.
    *
    * @param mathContextICU The MathContext to use when rounding numbers.
+   * @throws ArithmeticException when inverting multiplier produces a non-terminating decimal result
+   *         in conjunction with MathContext of unlimited precision.
    * @see #setMathContext(java.math.MathContext)
    * @category Rounding
    * @stable ICU 4.2
index 246c8aa975d79b7838a9692a5133211379350409..008bfedb34ad30de89e0175dc341814f7857225a 100644 (file)
@@ -4962,10 +4962,51 @@ public class NumberFormatTest extends TestFmwk {
         df.setMathContext(fourDigits);
         BigInteger actual4Digits = ((BigDecimal) df.parse(hugeNumberString)).toBigIntegerExact();
         assertEquals("Extreme division with fourDigits", huge4Digits, actual4Digits);
+    }
+
+    /**
+     * ArithmeticException is thrown when inverting multiplier produces a non-terminating
+     * decimal result in conjunction with MathContext of unlimited precision.
+     */
+    @Test
+    public void testSetMathContextArithmeticException() {
+        DecimalFormat df = new DecimalFormat();
+        df.setMultiplier(7);
+        try {
+            df.setMathContext(java.math.MathContext.UNLIMITED);
+            fail("Extreme division with unlimited precision should throw ArithmeticException");
+        } catch (ArithmeticException e) {
+            // expected
+        }
+    }
+
+    /**
+     * ArithmeticException is thrown when inverting multiplier produces a non-terminating
+     * decimal result in conjunction with MathContext of unlimited precision.
+     */
+    @Test
+    public void testSetMathContextICUArithmeticException() {
+        DecimalFormat df = new DecimalFormat();
+        df.setMultiplier(7);
+        try {
+            df.setMathContextICU(new MathContext(0));
+            fail("Extreme division with unlimited precision should throw ArithmeticException");
+        } catch (ArithmeticException e) {
+            // expected
+        }
+    }
+
+    /**
+     * ArithmeticException is thrown when inverting multiplier produces a non-terminating
+     * decimal result in conjunction with MathContext of unlimited precision.
+     */
+    @Test
+    public void testSetMultiplierArithmeticException() {
+        DecimalFormat df = new DecimalFormat();
+        df.setMathContext(java.math.MathContext.UNLIMITED);
         try {
-            df.setMathContext(unlimitedCeiling);
-            df.parse(hugeNumberString);
-            fail("Extreme division with unlimitedCeiling should throw ArithmeticException");
+            df.setMultiplier(7);
+            fail("Extreme division with unlimited precision should throw ArithmeticException");
         } catch (ArithmeticException e) {
             // expected
         }