ICU-10045 Formatting with significant digits to work correctly with 0 in C++

X-SVN-Rev: 33485
This commit is contained in:
Travis Keep 2013-04-02 22:01:33 +00:00
parent 5dfe1df049
commit 6125cf2eff
3 changed files with 63 additions and 1 deletions

View File

@ -1802,6 +1802,14 @@ DecimalFormat::subformat(UnicodeString& appendTo,
count = 0;
}
// This handles the special case of formatting 0. For zero only, we count the
// zero to the left of the decimal point as one signficant digit. Ordinarily we
// do not count any leading 0's as significant. If the number we are formatting
// is not zero, then either sigCount or digits.getDecimalAt() will be non-zero.
if (sigCount == 0 && digits.getDecimalAt() == 0) {
sigCount = 1;
}
for (i=0; i < count; ++i) {
// Here is where we escape from the loop. We escape
// if we've output the maximum fraction digits

View File

@ -118,6 +118,7 @@ void NumberFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &n
TESTCASE_AUTO(Test9087);
TESTCASE_AUTO(TestFormatFastpaths);
TESTCASE_AUTO(TestFormattableSize);
TESTCASE_AUTO(TestSignificantDigits);
TESTCASE_AUTO_END;
}
@ -6695,4 +6696,55 @@ void NumberFormatTest::TestFormattableSize(void) {
}
}
void NumberFormatTest::TestSignificantDigits(void) {
double input[] = {
0, 0,
123, -123,
12345, -12345,
123.45, -123.45,
123.44501, -123.44501,
0.001234, -0.001234,
0.00000000123, -0.00000000123,
0.0000000000000000000123, -0.0000000000000000000123,
1.2, -1.2,
0.0000000012344501, -0.0000000012344501,
123445.01, -123445.01,
12344501000000000000000000000000000.0, -12344501000000000000000000000000000.0,
};
const char* expected[] = {
"0.00", "0.00",
"123", "-123",
"12345", "-12345",
"123.45", "-123.45",
"123.45", "-123.45",
"0.001234", "-0.001234",
"0.00000000123", "-0.00000000123",
"0.0000000000000000000123", "-0.0000000000000000000123",
"1.20", "-1.20",
"0.0000000012345", "-0.0000000012345",
"123450", "-123450",
"12345000000000000000000000000000000", "-12345000000000000000000000000000000",
};
UErrorCode status = U_ZERO_ERROR;
Locale locale("en_US");
DecimalFormat* numberFormat = static_cast<DecimalFormat*>(
NumberFormat::createInstance(locale, status));
numberFormat->setSignificantDigitsUsed(TRUE);
numberFormat->setMinimumSignificantDigits(3);
numberFormat->setMaximumSignificantDigits(5);
numberFormat->setGroupingUsed(false);
UnicodeString result;
UnicodeString expectedResult;
for (int i = 0; i < sizeof(input)/sizeof(double); ++i) {
numberFormat->format(input[i], result);
expectedResult = UNICODE_STRING_SIMPLE(expected[i]);
if (result != expectedResult) {
errln((UnicodeString)"Expected: '" + expectedResult + "' got '" + result);
}
result.remove();
}
}
#endif /* #if !UCONFIG_NO_FORMATTING */

View File

@ -1,6 +1,6 @@
/************************************************************************
* COPYRIGHT:
* Copyright (c) 1997-2012, International Business Machines Corporation
* Copyright (c) 1997-2013, International Business Machines Corporation
* and others. All Rights Reserved.
************************************************************************/
@ -162,6 +162,8 @@ class NumberFormatTest: public CalendarTimeZoneTest {
void TestEnumSet();
void TestSignificantDigits();
private:
static UBool equalValue(const Formattable& a, const Formattable& b);