diff --git a/icu4c/source/i18n/number_fluent.cpp b/icu4c/source/i18n/number_fluent.cpp index bc43defaa3..c92c6bd0be 100644 --- a/icu4c/source/i18n/number_fluent.cpp +++ b/icu4c/source/i18n/number_fluent.cpp @@ -732,7 +732,17 @@ int32_t LocalizedNumberFormatter::getCallCount() const { UnicodeString FormattedNumber::toString() const { if (fResults == nullptr) { - // TODO: http://bugs.icu-project.org/trac/ticket/13437 + return {}; + } + return fResults->string.toUnicodeString(); +} + +UnicodeString FormattedNumber::toString(UErrorCode& status) const { + if (U_FAILURE(status)) { + return {}; + } + if (fResults == nullptr) { + status = fErrorCode; return {}; } return fResults->string.toUnicodeString(); @@ -740,7 +750,18 @@ UnicodeString FormattedNumber::toString() const { Appendable& FormattedNumber::appendTo(Appendable& appendable) { if (fResults == nullptr) { - // TODO: http://bugs.icu-project.org/trac/ticket/13437 + return appendable; + } + appendable.appendString(fResults->string.chars(), fResults->string.length()); + return appendable; +} + +Appendable& FormattedNumber::appendTo(Appendable& appendable, UErrorCode& status) { + if (U_FAILURE(status)) { + return appendable; + } + if (fResults == nullptr) { + status = fErrorCode; return appendable; } appendable.appendString(fResults->string.chars(), fResults->string.length()); @@ -748,7 +769,9 @@ Appendable& FormattedNumber::appendTo(Appendable& appendable) { } void FormattedNumber::populateFieldPosition(FieldPosition& fieldPosition, UErrorCode& status) { - if (U_FAILURE(status)) { return; } + if (U_FAILURE(status)) { + return; + } if (fResults == nullptr) { status = fErrorCode; return; @@ -757,7 +780,9 @@ void FormattedNumber::populateFieldPosition(FieldPosition& fieldPosition, UError } void FormattedNumber::populateFieldPositionIterator(FieldPositionIterator& iterator, UErrorCode& status) { - if (U_FAILURE(status)) { return; } + if (U_FAILURE(status)) { + return; + } if (fResults == nullptr) { status = fErrorCode; return; @@ -766,7 +791,9 @@ void FormattedNumber::populateFieldPositionIterator(FieldPositionIterator& itera } void FormattedNumber::getDecimalQuantity(DecimalQuantity& output, UErrorCode& status) const { - if (U_FAILURE(status)) { return; } + if (U_FAILURE(status)) { + return; + } if (fResults == nullptr) { status = fErrorCode; return; diff --git a/icu4c/source/i18n/unicode/numberformatter.h b/icu4c/source/i18n/unicode/numberformatter.h index d09ab365a8..493051eb74 100644 --- a/icu4c/source/i18n/unicode/numberformatter.h +++ b/icu4c/source/i18n/unicode/numberformatter.h @@ -2320,21 +2320,48 @@ class U_I18N_API FormattedNumber : public UMemory { * Returns a UnicodeString representation of the formatted number. * * @return a UnicodeString containing the localized number. - * @draft ICU 60 + * @deprecated ICU 62 Use the version of this method with an error code instead. + * This method was never @stable and will be removed in a future release. + * See http://bugs.icu-project.org/trac/ticket/13746 */ UnicodeString toString() const; + /** + * Returns a UnicodeString representation of the formatted number. + * + * @param status + * Set if an error occurs while formatting the number to the UnicodeString. + * @return a UnicodeString containing the localized number. + * @draft ICU 62 + */ + UnicodeString toString(UErrorCode& status) const; + /** * Appends the formatted number to an Appendable. * * @param appendable * The Appendable to which to append the formatted number string. * @return The same Appendable, for chaining. - * @draft ICU 60 + * @deprecated ICU 62 Use the version of this method with an error code instead. + * This method was never @stable and will be removed in a future release. + * See http://bugs.icu-project.org/trac/ticket/13746 * @see Appendable */ Appendable &appendTo(Appendable &appendable); + /** + * Appends the formatted number to an Appendable. + * + * @param appendable + * The Appendable to which to append the formatted number string. + * @param status + * Set if an error occurs while formatting the number to the Appendable. + * @return The same Appendable, for chaining. + * @draft ICU 62 + * @see Appendable + */ + Appendable &appendTo(Appendable &appendable, UErrorCode& status); + /** * Determine the start and end indices of the first occurrence of the given field in the output string. * This allows you to determine the locations of the integer part, fraction part, and sign. diff --git a/icu4c/source/test/intltest/numbertest_api.cpp b/icu4c/source/test/intltest/numbertest_api.cpp index f374d8e3e3..6561172995 100644 --- a/icu4c/source/test/intltest/numbertest_api.cpp +++ b/icu4c/source/test/intltest/numbertest_api.cpp @@ -2071,24 +2071,81 @@ void NumberFormatterApiTest::errors() { Rounder::fixedFraction( -1)); - { - UErrorCode status1 = U_ZERO_ERROR; - UErrorCode status2 = U_ZERO_ERROR; - FormattedNumber fn = lnf.formatInt(1, status1); - assertEquals( - "Should fail since rounder is not legal", U_NUMBER_ARG_OUTOFBOUNDS_ERROR, status1); - FieldPosition fp; - fn.populateFieldPosition(fp, status2); - assertEquals( - "Should fail on terminal method", U_NUMBER_ARG_OUTOFBOUNDS_ERROR, status2); - } + // formatInt + UErrorCode status = U_ZERO_ERROR; + FormattedNumber fn = lnf.formatInt(1, status); + assertEquals( + "Should fail in formatInt method with error code for rounding", + U_NUMBER_ARG_OUTOFBOUNDS_ERROR, + status); - { - UErrorCode status = U_ZERO_ERROR; - lnf.copyErrorTo(status); - assertEquals( - "Should fail since rounder is not legal", U_NUMBER_ARG_OUTOFBOUNDS_ERROR, status); - } + // formatDouble + status = U_ZERO_ERROR; + fn = lnf.formatDouble(1.0, status); + assertEquals( + "Should fail in formatDouble method with error code for rounding", + U_NUMBER_ARG_OUTOFBOUNDS_ERROR, + status); + + // formatDecimal (decimal error) + status = U_ZERO_ERROR; + fn = NumberFormatter::withLocale("en").formatDecimal("1x2", status); + assertEquals( + "Should fail in formatDecimal method with error code for decimal number syntax", + U_DECIMAL_NUMBER_SYNTAX_ERROR, + status); + + // formatDecimal (setting error) + status = U_ZERO_ERROR; + fn = lnf.formatDecimal("1.0", status); + assertEquals( + "Should fail in formatDecimal method with error code for rounding", + U_NUMBER_ARG_OUTOFBOUNDS_ERROR, + status); + + // FieldPosition + status = U_ZERO_ERROR; + FieldPosition fp; + fn.populateFieldPosition(fp, status); + assertEquals( + "Should fail on FieldPosition terminal method with correct error code", + U_NUMBER_ARG_OUTOFBOUNDS_ERROR, + status); + + // FieldPositionIterator + status = U_ZERO_ERROR; + FieldPositionIterator fpi; + fn.populateFieldPositionIterator(fpi, status); + assertEquals( + "Should fail on FieldPositoinIterator terminal method with correct error code", + U_NUMBER_ARG_OUTOFBOUNDS_ERROR, + status); + + // Appendable + status = U_ZERO_ERROR; + UnicodeString output; + UnicodeStringAppendable appendable(output); + fn.appendTo(appendable, status); + assertEquals( + "Should fail on Appendable terminal method with correct error code", + U_NUMBER_ARG_OUTOFBOUNDS_ERROR, + status); + + // UnicodeString + status = U_ZERO_ERROR; + output = fn.toString(status); + assertEquals( + "Should fail on UnicodeString terminal method with correct error code", + U_NUMBER_ARG_OUTOFBOUNDS_ERROR, + status); + + // CopyErrorTo + status = U_ZERO_ERROR; + lnf.copyErrorTo(status); + assertEquals( + "Should fail since rounder is not legal with correct error code", + U_NUMBER_ARG_OUTOFBOUNDS_ERROR, + status); } void NumberFormatterApiTest::validRanges() {