]> granicus.if.org Git - icu/commitdiff
ICU-10547 Format -0.0 as a negative when using scientific notation.
authorTravis Keep <keep94@gmail.com>
Fri, 21 Feb 2014 20:41:39 +0000 (20:41 +0000)
committerTravis Keep <keep94@gmail.com>
Fri, 21 Feb 2014 20:41:39 +0000 (20:41 +0000)
X-SVN-Rev: 35196

icu4c/source/i18n/decimfmt.cpp
icu4c/source/test/intltest/numfmtst.cpp
icu4c/source/test/intltest/numfmtst.h

index d6864dc6fa39163ba1ef8cb6d4417a68fed50592..4ac3f640bb76c55e2f8672574900e2fd4b942285 100644 (file)
@@ -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.
index a5c50108737421b80d6fb0ad000dd6c248fe30dd..ab952ca39f7e006b3d56c4e65a62c0e035c22418 100644 (file)
@@ -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,
index 28cf5a2d2739ce0e709c192561cdf0f3352258ac..f220f20c708a712f07882a9f6a3521597adfafa5 100644 (file)
@@ -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);