ICU-21881 Fix TrailingZeroDisplay with RoundingMode

See #1977
This commit is contained in:
Shane F. Carr 2022-02-23 17:38:24 +00:00 committed by Shane F. Carr
parent 81dfbdf3b6
commit 3aa8f9fda1
3 changed files with 64 additions and 10 deletions

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

View 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;
}
}

View 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