]> granicus.if.org Git - icu/commitdiff
ICU-20620 cap UNUM_MAX_FRACTION_DIGITS setting at 999
authorPeter Edberg <pedberg@unicode.org>
Wed, 28 Aug 2019 06:19:19 +0000 (23:19 -0700)
committerpedberg-icu <42151464+pedberg-icu@users.noreply.github.com>
Thu, 29 Aug 2019 23:12:09 +0000 (16:12 -0700)
icu4c/source/i18n/decimfmt.cpp
icu4c/source/test/cintltst/cnumtst.c

index cb82e7f654a637a1554df7d5a18a74578fc034a7..d19f65188804986a86bfd576632f4cbc93727241 100644 (file)
@@ -1388,6 +1388,10 @@ void DecimalFormat::setMinimumIntegerDigits(int32_t newValue) {
 void DecimalFormat::setMaximumFractionDigits(int32_t newValue) {
     if (fields == nullptr) { return; }
     if (newValue == fields->properties.maximumFractionDigits) { return; }
+    // cap for backward compatibility, formerly 340, now 999
+    if (newValue > kMaxIntFracSig) {
+        newValue = kMaxIntFracSig;
+    }
     // For backwards compatibility, conflicting min/max need to keep the most recent setting.
     int32_t min = fields->properties.minimumFractionDigits;
     if (min >= 0 && min > newValue) {
index c1a6132d2b87477e14b4dffd4c322c1a290ae203..6ad0bf1d1bae5853bd6697ab6c2f5fca2076a7f0 100644 (file)
@@ -73,6 +73,7 @@ static void Test12052_NullPointer(void);
 static void TestParseCases(void);
 static void TestSetMaxFracAndRoundIncr(void);
 static void TestIgnorePadding(void);
+static void TestSciNotationMaxFracCap(void);
 
 #define TESTCASE(x) addTest(root, &x, "tsformat/cnumtst/" #x)
 
@@ -112,6 +113,7 @@ void addNumForTest(TestNode** root)
     TESTCASE(TestParseCases);
     TESTCASE(TestSetMaxFracAndRoundIncr);
     TESTCASE(TestIgnorePadding);
+    TESTCASE(TestSciNotationMaxFracCap);
 }
 
 /* test Parse int 64 */
@@ -3439,4 +3441,38 @@ static void TestIgnorePadding(void) {
     }
 }
 
+static void TestSciNotationMaxFracCap(void) {
+    static const UChar* pat1 = u"#.##E+00;-#.##E+00";
+    UErrorCode status = U_ZERO_ERROR;
+    UNumberFormat* unum = unum_open(UNUM_PATTERN_DECIMAL, pat1, -1, "en_US", NULL, &status);
+    if ( U_FAILURE(status) ) {
+        log_data_err("unum_open UNUM_PATTERN_DECIMAL with scientific pattern for \"en_US\" fails with %s\n", u_errorName(status));
+    } else {
+        double value;
+        UChar ubuf[kUBufMax];
+        char bbuf[kBBufMax];
+        int32_t ulen;
+
+        unum_setAttribute(unum, UNUM_MIN_FRACTION_DIGITS, 0);
+        unum_setAttribute(unum, UNUM_MAX_FRACTION_DIGITS, 2147483647);
+        ulen = unum_toPattern(unum, FALSE, ubuf, kUBufMax, &status);
+        if ( U_SUCCESS(status) ) {
+            u_austrncpy(bbuf, ubuf, kUBufMax);
+            log_info("unum_toPattern (%d): %s\n", ulen, bbuf);
+        }
+
+        for (value = 10.0; value < 1000000000.0; value *= 10.0) {
+            status = U_ZERO_ERROR;
+            ulen = unum_formatDouble(unum, value, ubuf, kUBufMax, NULL, &status);
+            if ( U_FAILURE(status) ) {
+                log_err("unum_formatDouble value %.1f status %s\n", value, u_errorName(status));
+            } else if (u_strncmp(ubuf,u"1E+0",4) != 0) {
+                u_austrncpy(bbuf, ubuf, kUBufMax);
+                log_err("unum_formatDouble value %.1f expected result to begin with 1E+0, got %s\n", value, bbuf);
+            }
+        }
+        unum_close(unum);
+    }
+}
+
 #endif /* #if !UCONFIG_NO_FORMATTING */