]> granicus.if.org Git - icu/commitdiff
ICU-21881 Fix TrailingZeroDisplay with RoundingMode
authorShane F. Carr <sffc@google.com>
Wed, 23 Feb 2022 17:38:24 +0000 (17:38 +0000)
committerShane F. Carr <shane@unicode.org>
Wed, 23 Feb 2022 22:46:54 +0000 (15:46 -0700)
See #1977

icu4c/source/test/intltest/numbertest_api.cpp
icu4j/main/classes/core/src/com/ibm/icu/number/Precision.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/number/NumberFormatterApiTest.java

index 574ee958ffc0f6aaa04190d5aaad437255b9d304..2e35db8f373d81e4c1bd2fbadab00bb826ebb559 100644 (file)
@@ -3012,6 +3012,28 @@ void NumberFormatterApiTest::roundingFraction() {
             Locale::getEnglish(),
             1,
             "1");
+
+    assertFormatSingle(
+            u"Hide If Whole with Rounding Mode A (ICU-21881)",
+            u".00/w rounding-mode-floor",
+            u".00/w rounding-mode-floor",
+            NumberFormatter::with().precision(Precision::fixedFraction(2)
+                .trailingZeroDisplay(UNUM_TRAILING_ZERO_HIDE_IF_WHOLE))
+                .roundingMode(UNUM_ROUND_FLOOR),
+            Locale::getEnglish(),
+            3.009,
+            "3");
+
+    assertFormatSingle(
+            u"Hide If Whole with Rounding Mode B (ICU-21881)",
+            u".00/w rounding-mode-half-up",
+            u".00/w rounding-mode-half-up",
+            NumberFormatter::with().precision(Precision::fixedFraction(2)
+                .trailingZeroDisplay(UNUM_TRAILING_ZERO_HIDE_IF_WHOLE))
+                .roundingMode(UNUM_ROUND_HALFUP),
+            Locale::getEnglish(),
+            3.001,
+            "3");
 }
 
 void NumberFormatterApiTest::roundingFigures() {
index 916fa733a4bf97643467d1fc1cd4ebb01349951e..6cdabfbe678b0293bf4916db926551d4e20bf6b4 100644 (file)
@@ -371,6 +371,16 @@ public abstract class Precision {
     /** Package-private clone method */
     abstract Precision createCopy();
 
+    /**
+     * Call this function to copy the fields from the Precision base class.
+     *
+     * Note: It would be nice if this returned the copy, but most impls return the child class, not Precision.
+     */
+    /* package-private */ void createCopyHelper(Precision copy) {
+        copy.mathContext = mathContext;
+        copy.trailingZeroDisplay = trailingZeroDisplay;
+    }
+
     /**
      * @internal
      * @deprecated ICU 60 This API is ICU internal only.
@@ -602,7 +612,7 @@ public abstract class Precision {
         @Override
         BogusRounder createCopy() {
             BogusRounder copy = new BogusRounder();
-            copy.mathContext = mathContext;
+            createCopyHelper(copy);
             return copy;
         }
 
@@ -615,7 +625,7 @@ public abstract class Precision {
         @Deprecated
         public Precision into(Precision precision) {
             Precision copy = precision.createCopy();
-            copy.mathContext = mathContext;
+            createCopyHelper(copy);
             return copy;
         }
     }
@@ -634,7 +644,7 @@ public abstract class Precision {
         @Override
         InfiniteRounderImpl createCopy() {
             InfiniteRounderImpl copy = new InfiniteRounderImpl();
-            copy.mathContext = mathContext;
+            createCopyHelper(copy);
             return copy;
         }
     }
@@ -657,7 +667,7 @@ public abstract class Precision {
         @Override
         FractionRounderImpl createCopy() {
             FractionRounderImpl copy = new FractionRounderImpl(minFrac, maxFrac);
-            copy.mathContext = mathContext;
+            createCopyHelper(copy);
             return copy;
         }
     }
@@ -693,7 +703,7 @@ public abstract class Precision {
         @Override
         SignificantRounderImpl createCopy() {
             SignificantRounderImpl copy = new SignificantRounderImpl(minSig, maxSig);
-            copy.mathContext = mathContext;
+            createCopyHelper(copy);
             return copy;
         }
     }
@@ -761,7 +771,7 @@ public abstract class Precision {
         @Override
         FracSigRounderImpl createCopy() {
             FracSigRounderImpl copy = new FracSigRounderImpl(minFrac, maxFrac, minSig, maxSig, priority, retain);
-            copy.mathContext = mathContext;
+            createCopyHelper(copy);
             return copy;
         }
     }
@@ -785,7 +795,7 @@ public abstract class Precision {
         @Override
         IncrementRounderImpl createCopy() {
             IncrementRounderImpl copy = new IncrementRounderImpl(increment);
-            copy.mathContext = mathContext;
+            createCopyHelper(copy);
             return copy;
         }
     }
@@ -814,7 +824,7 @@ public abstract class Precision {
         @Override
         IncrementOneRounderImpl createCopy() {
             IncrementOneRounderImpl copy = new IncrementOneRounderImpl(increment, minFrac, maxFrac);
-            copy.mathContext = mathContext;
+            createCopyHelper(copy);
             return copy;
         }
     }
@@ -841,7 +851,7 @@ public abstract class Precision {
         @Override
         IncrementFiveRounderImpl createCopy() {
             IncrementFiveRounderImpl copy = new IncrementFiveRounderImpl(increment, minFrac, maxFrac);
-            copy.mathContext = mathContext;
+            createCopyHelper(copy);
             return copy;
         }
     }
@@ -862,7 +872,7 @@ public abstract class Precision {
         @Override
         CurrencyRounderImpl createCopy() {
             CurrencyRounderImpl copy = new CurrencyRounderImpl(usage);
-            copy.mathContext = mathContext;
+            createCopyHelper(copy);
             return copy;
         }
     }
index 5d6dde5aa489a6e0effc77bb8f1bc14a6a3af90d..ed175af3b169a7bba8e01b222474f3335709314d 100644 (file)
@@ -2991,6 +2991,28 @@ public class NumberFormatterApiTest extends TestFmwk {
                 ULocale.ENGLISH,
                 1,
                 "1");
+
+        assertFormatSingle(
+                "Hide If Whole with Rounding Mode A (ICU-21881)",
+                ".00/w rounding-mode-floor",
+                ".00/w rounding-mode-floor",
+                NumberFormatter.with().precision(Precision.fixedFraction(2)
+                    .trailingZeroDisplay(TrailingZeroDisplay.HIDE_IF_WHOLE))
+                    .roundingMode(RoundingMode.FLOOR),
+                ULocale.ENGLISH,
+                3.009,
+                "3");
+
+        assertFormatSingle(
+                "Hide If Whole with Rounding Mode B (ICU-21881)",
+                ".00/w rounding-mode-half-up",
+                ".00/w rounding-mode-half-up",
+                NumberFormatter.with().precision(Precision.fixedFraction(2)
+                    .trailingZeroDisplay(TrailingZeroDisplay.HIDE_IF_WHOLE))
+                    .roundingMode(RoundingMode.HALF_UP),
+                ULocale.ENGLISH,
+                3.001,
+                "3");
     }
 
     @Test