From 18b6e9fc60e2c3d13756abb6b904fe5e72b94157 Mon Sep 17 00:00:00 2001 From: Travis Keep Date: Thu, 10 Oct 2013 21:49:53 +0000 Subject: [PATCH] ICU-10419 Fix rounding in NumberFormat. X-SVN-Rev: 34549 --- icu4c/source/test/intltest/numfmtst.cpp | 35 +++++++++++++++++++++++++ icu4c/source/test/intltest/numfmtst.h | 1 + 2 files changed, 36 insertions(+) diff --git a/icu4c/source/test/intltest/numfmtst.cpp b/icu4c/source/test/intltest/numfmtst.cpp index 2a0406a13a..6ae2737008 100644 --- a/icu4c/source/test/intltest/numfmtst.cpp +++ b/icu4c/source/test/intltest/numfmtst.cpp @@ -130,6 +130,7 @@ void NumberFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &n TESTCASE_AUTO(TestParseNegativeWithAlternateMinusSign); TESTCASE_AUTO(TestCustomCurrencySignAndSeparator); TESTCASE_AUTO(TestParseSignsAndMarks); + TESTCASE_AUTO(Test10419RoundingWith0FractionDigits); TESTCASE_AUTO_END; } @@ -7284,4 +7285,38 @@ void NumberFormatTest::TestParseSignsAndMarks() { } } +typedef struct { + DecimalFormat::ERoundingMode mode; + double value; + UnicodeString expected; +} Test10419Data; + + +// Tests that rounding works right when fractional digits is set to 0. +void NumberFormatTest::Test10419RoundingWith0FractionDigits() { + const Test10419Data items[] = { + { DecimalFormat::kRoundCeiling, 1.488, "2"}, + { DecimalFormat::kRoundDown, 1.588, "1"}, + { DecimalFormat::kRoundFloor, 1.888, "1"}, + { DecimalFormat::kRoundHalfDown, 1.5, "1"}, + { DecimalFormat::kRoundHalfEven, 2.5, "2"}, + { DecimalFormat::kRoundHalfUp, 2.5, "3"}, + { DecimalFormat::kRoundUp, 1.5, "2"}, + }; + UErrorCode status = U_ZERO_ERROR; + LocalPointer decfmt((DecimalFormat *) NumberFormat::createInstance(Locale("en_US"), status)); + if (U_FAILURE(status)) { + dataerrln("Failure creating DecimalFormat %s", u_errorName(status)); + return; + } + for (int32_t i = 0; i < sizeof(items) / sizeof(items[0]); ++i) { + decfmt->setRoundingMode(items[i].mode); + decfmt->setMaximumFractionDigits(0); + UnicodeString actual; + if (items[i].expected != decfmt->format(items[i].value, actual)) { + errln("Expected " + items[i].expected + ", got " + actual); + } + } +} + #endif /* #if !UCONFIG_NO_FORMATTING */ diff --git a/icu4c/source/test/intltest/numfmtst.h b/icu4c/source/test/intltest/numfmtst.h index fad385974d..9e3eada2e4 100644 --- a/icu4c/source/test/intltest/numfmtst.h +++ b/icu4c/source/test/intltest/numfmtst.h @@ -175,6 +175,7 @@ class NumberFormatTest: public CalendarTimeZoneTest { void TestCustomCurrencySignAndSeparator(); void TestParseSignsAndMarks(); + void Test10419RoundingWith0FractionDigits(); private: UBool testFormattableAsUFormattable(const char *file, int line, Formattable &f);