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
This commit is contained in:
Michael Grady 2010-06-04 05:42:19 +00:00
parent 79d1fa387f
commit fa477f8a22
3 changed files with 53 additions and 12 deletions

View File

@ -1108,19 +1108,17 @@ DecimalFormat::_format(const DigitList &number,
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);
}

View File

@ -44,6 +44,7 @@ void addNumForTest(TestNode** root)
TESTCASE(TestNumberFormat);
TESTCASE(TestSpelloutNumberParse);
TESTCASE(TestSignificantDigits);
TESTCASE(TestSigDigRounding);
TESTCASE(TestNumberFormatPadding);
TESTCASE(TestInt64Format);
TESTCASE(TestNonExistentCurrency);
@ -886,6 +887,43 @@ static void TestSignificantDigits()
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;

View File

@ -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.
********************************************************************/
/********************************************************************************
@ -38,6 +38,11 @@ static void TestSpelloutNumberParse(void);
**/
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
**/