From: Peter Edberg Date: Thu, 30 Jun 2011 05:43:56 +0000 (+0000) Subject: ICU-8593 RuleBasedNumberFormat::format(double,...) should handle NAN X-Git-Tag: milestone-59-0-1~4700 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1d01381d475eb358f752fa1353181888e182b0c4;p=icu ICU-8593 RuleBasedNumberFormat::format(double,...) should handle NAN X-SVN-Rev: 30258 --- diff --git a/icu4c/source/i18n/rbnf.cpp b/icu4c/source/i18n/rbnf.cpp index 2e20b92bc08..4b88869a230 100644 --- a/icu4c/source/i18n/rbnf.cpp +++ b/icu4c/source/i18n/rbnf.cpp @@ -1067,7 +1067,15 @@ RuleBasedNumberFormat::format(double number, UnicodeString& toAppendTo, FieldPosition& /* pos */) const { - if (defaultRuleSet) defaultRuleSet->format(number, toAppendTo, toAppendTo.length()); + // Special case for NaN; adapted from what DecimalFormat::_format( double number,...) does. + if (uprv_isNaN(number)) { + DecimalFormatSymbols* decFmtSyms = getDecimalFormatSymbols(); // RuleBasedNumberFormat internal + if (decFmtSyms) { + toAppendTo += decFmtSyms->getConstSymbol(DecimalFormatSymbols::kNaNSymbol); + } + } else if (defaultRuleSet) { + defaultRuleSet->format(number, toAppendTo, toAppendTo.length()); + } return toAppendTo; } diff --git a/icu4c/source/test/cintltst/cnumtst.c b/icu4c/source/test/cintltst/cnumtst.c index cc9963ef454..cd371ca90be 100644 --- a/icu4c/source/test/cintltst/cnumtst.c +++ b/icu4c/source/test/cintltst/cnumtst.c @@ -1636,6 +1636,23 @@ static void TestRBNFFormat() { test_fmt(formats[i], (UBool)(i == 0)); } + #define FORMAT_BUF_CAPACITY 64 + { + UChar fmtbuf[FORMAT_BUF_CAPACITY]; + int32_t len; + double nanvalue = uprv_getNaN(); + status = U_ZERO_ERROR; + len = unum_formatDouble(formats[1], nanvalue, fmtbuf, FORMAT_BUF_CAPACITY, NULL, &status); + if (U_FAILURE(status)) { + log_err_status(status, "unum_formatDouble NAN failed with %s\n", u_errorName(status)); + } else { + UChar nansym[] = { 0x4E, 0x61, 0x4E, 0 }; /* NaN */ + if ( len != 3 || u_strcmp(fmtbuf, nansym) != 0 ) { + log_err("unum_formatDouble NAN produced wrong answer for en_US\n"); + } + } + } + for (i = 0; i < COUNT; ++i) { unum_close(formats[i]); }