From: Shane F. Carr Date: Wed, 23 Feb 2022 17:38:24 +0000 (+0000) Subject: ICU-21881 Fix TrailingZeroDisplay with RoundingMode X-Git-Tag: release-71-rc~47 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3aa8f9fda161b1639c5628350db2e0992b0bb7bd;p=icu ICU-21881 Fix TrailingZeroDisplay with RoundingMode See #1977 --- diff --git a/icu4c/source/test/intltest/numbertest_api.cpp b/icu4c/source/test/intltest/numbertest_api.cpp index 574ee958ffc..2e35db8f373 100644 --- a/icu4c/source/test/intltest/numbertest_api.cpp +++ b/icu4c/source/test/intltest/numbertest_api.cpp @@ -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() { diff --git a/icu4j/main/classes/core/src/com/ibm/icu/number/Precision.java b/icu4j/main/classes/core/src/com/ibm/icu/number/Precision.java index 916fa733a4b..6cdabfbe678 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/number/Precision.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/number/Precision.java @@ -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; } } diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/number/NumberFormatterApiTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/number/NumberFormatterApiTest.java index 5d6dde5aa48..ed175af3b16 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/number/NumberFormatterApiTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/number/NumberFormatterApiTest.java @@ -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