From fcde5a4621a29e9d15ee95ba34c0b3ac056a2c44 Mon Sep 17 00:00:00 2001 From: Travis Keep Date: Fri, 21 Feb 2014 20:41:39 +0000 Subject: [PATCH] ICU-10547 Format -0.0 as a negative when using scientific notation. X-SVN-Rev: 35196 --- icu4c/source/i18n/decimfmt.cpp | 7 +++++++ icu4c/source/test/intltest/numfmtst.cpp | 18 ++++++++++++++++++ icu4c/source/test/intltest/numfmtst.h | 3 ++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/icu4c/source/i18n/decimfmt.cpp b/icu4c/source/i18n/decimfmt.cpp index d6864dc6fa3..4ac3f640bb7 100644 --- a/icu4c/source/i18n/decimfmt.cpp +++ b/icu4c/source/i18n/decimfmt.cpp @@ -1643,6 +1643,13 @@ DecimalFormat::_round(const DigitList &number, DigitList &adjustedNum, UBool& is int32_t sigDigits = precision(); if (sigDigits > 0) { adjustedNum.round(sigDigits); + // Travis Keep (21/2/2014): Calling round on a digitList does not necessarily + // preserve the sign of that digit list. Preserving the sign is especially + // important when formatting -0.0 for instance. Not preserving the sign seems + // like a bug because I cannot think of any case where the sign would actually + // have to change when rounding. For now, we preserve the sign by setting the + // positive attribute directly. + adjustedNum.setPositive(!isNegative); } } else { // Fixed point format. Round to a set number of fraction digits. diff --git a/icu4c/source/test/intltest/numfmtst.cpp b/icu4c/source/test/intltest/numfmtst.cpp index a5c50108737..ab952ca39f7 100644 --- a/icu4c/source/test/intltest/numfmtst.cpp +++ b/icu4c/source/test/intltest/numfmtst.cpp @@ -131,6 +131,7 @@ void NumberFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &n TESTCASE_AUTO(Test10419RoundingWith0FractionDigits); TESTCASE_AUTO(Test10468ApplyPattern); TESTCASE_AUTO(TestRoundingScientific10542); + TESTCASE_AUTO(TestZeroScientific10547); TESTCASE_AUTO_END; } @@ -7539,6 +7540,23 @@ void NumberFormatTest::TestRoundingScientific10542() { } } +void NumberFormatTest::TestZeroScientific10547() { + UErrorCode status = U_ZERO_ERROR; + { + DecimalFormat fmt("0.00", status); + UnicodeString out; + fmt.format(-0.0, out); + assertEquals("formatFixed", "-0.00", out); + } + DecimalFormat fmt("0.00E0", status); + if (!assertSuccess("Formt creation", status)) { + return; + } + UnicodeString out; + fmt.format(-0.0, out); + assertEquals("format", "-0.00E0", out); +} + void NumberFormatTest::verifyRounding( DecimalFormat& format, const double *values, diff --git a/icu4c/source/test/intltest/numfmtst.h b/icu4c/source/test/intltest/numfmtst.h index 28cf5a2d273..f220f20c708 100644 --- a/icu4c/source/test/intltest/numfmtst.h +++ b/icu4c/source/test/intltest/numfmtst.h @@ -1,6 +1,6 @@ /************************************************************************ * COPYRIGHT: - * Copyright (c) 1997-2013, International Business Machines Corporation + * Copyright (c) 1997-2014, International Business Machines Corporation * and others. All Rights Reserved. ************************************************************************/ @@ -178,6 +178,7 @@ class NumberFormatTest: public CalendarTimeZoneTest { void Test10419RoundingWith0FractionDigits(); void Test10468ApplyPattern(); void TestRoundingScientific10542(); + void TestZeroScientific10547(); private: UBool testFormattableAsUFormattable(const char *file, int line, Formattable &f); -- 2.40.0