ICU-7667 In DecimalFormat::_format, deal with cases where two forms of rounding are specified - number of digits _and_ a rounding increment
X-SVN-Rev: 28132
diff --git a/icu4c/source/i18n/decimfmt.cpp b/icu4c/source/i18n/decimfmt.cpp
index 46f3935..ba72cfc 100644
--- a/icu4c/source/i18n/decimfmt.cpp
+++ b/icu4c/source/i18n/decimfmt.cpp
@@ -1108,19 +1108,17 @@
return appendTo;
}
- if (fRoundingIncrement == NULL) {
- if (fUseExponentialNotation || areSignificantDigitsUsed()) {
- int32_t sigDigits = precision();
- if (sigDigits > 0) {
- adjustedNum.round(sigDigits);
- }
- } else {
- // Fixed point format. Round to a set number of fraction digits.
- int32_t numFractionDigits = precision();
- adjustedNum.roundFixedPoint(numFractionDigits);
+ if (fUseExponentialNotation || areSignificantDigitsUsed()) {
+ int32_t sigDigits = precision();
+ if (sigDigits > 0) {
+ adjustedNum.round(sigDigits);
}
+ } else {
+ // Fixed point format. Round to a set number of fraction digits.
+ int32_t numFractionDigits = precision();
+ adjustedNum.roundFixedPoint(numFractionDigits);
}
-
+
return subformat(appendTo, handler, adjustedNum, FALSE);
}
diff --git a/icu4c/source/test/cintltst/cnumtst.c b/icu4c/source/test/cintltst/cnumtst.c
index 571d789..762dd8a 100644
--- a/icu4c/source/test/cintltst/cnumtst.c
+++ b/icu4c/source/test/cintltst/cnumtst.c
@@ -44,6 +44,7 @@
TESTCASE(TestNumberFormat);
TESTCASE(TestSpelloutNumberParse);
TESTCASE(TestSignificantDigits);
+ TESTCASE(TestSigDigRounding);
TESTCASE(TestNumberFormatPadding);
TESTCASE(TestInt64Format);
TESTCASE(TestNonExistentCurrency);
@@ -886,6 +887,43 @@
unum_close(fmt);
}
+static void TestSigDigRounding()
+{
+ UErrorCode status = U_ZERO_ERROR;
+ UChar expected[128];
+ UChar result[128];
+ char temp1[128];
+ char temp2[128];
+ UNumberFormat* fmt;
+ double d = 123.4;
+
+ fmt=unum_open(UNUM_DECIMAL, NULL, 0, NULL /* "en_US"*/, NULL, &status);
+ if (U_FAILURE(status)) {
+ log_err("got unexpected error for unum_open: '%s'\n", u_errorName(status));
+ return;
+ }
+ unum_setAttribute(fmt, UNUM_LENIENT_PARSE, FALSE);
+ unum_setAttribute(fmt, UNUM_SIGNIFICANT_DIGITS_USED, TRUE);
+ unum_setAttribute(fmt, UNUM_MAX_SIGNIFICANT_DIGITS, 2);
+// unum_setAttribute(fmt, UNUM_MAX_FRACTION_DIGITS, 0);
+
+ unum_setAttribute(fmt, UNUM_ROUNDING_MODE, UNUM_ROUND_UP);
+ unum_setDoubleAttribute(fmt, UNUM_ROUNDING_INCREMENT, 20.0);
+
+ (void)unum_formatDouble(fmt, d, result, sizeof(result) / sizeof(result[0]), NULL, &status);
+ if(U_FAILURE(status))
+ {
+ log_err("Error in formatting using unum_formatDouble(.....): %s\n", myErrorName(status));
+ return;
+ }
+
+ u_uastrcpy(expected, "140");
+ if(u_strcmp(result, expected)!=0)
+ log_err("FAIL: Error in unum_formatDouble result %s instead of %s\n", u_austrcpy(temp1, result), u_austrcpy(temp2, expected) );
+
+ unum_close(fmt);
+}
+
static void TestNumberFormatPadding()
{
UChar *result=NULL;
diff --git a/icu4c/source/test/cintltst/cnumtst.h b/icu4c/source/test/cintltst/cnumtst.h
index 1a95fee..d4c051f 100644
--- a/icu4c/source/test/cintltst/cnumtst.h
+++ b/icu4c/source/test/cintltst/cnumtst.h
@@ -1,6 +1,6 @@
/********************************************************************
* COPYRIGHT:
- * Copyright (c) 1997-2009, International Business Machines Corporation and
+ * Copyright (c) 1997-2010, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
/********************************************************************************
@@ -39,6 +39,11 @@
static void TestSignificantDigits(void);
/**
+ * The function used to test Number format API rounding with significant digits
+ **/
+static void TestSigDigRounding(void);
+
+/**
* The function used to test the Number format API with padding
**/
static void TestNumberFormatPadding(void);