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();
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());
}
void FormattedNumber::populateFieldPosition(FieldPosition& fieldPosition, UErrorCode& status) {
- if (U_FAILURE(status)) { return; }
+ if (U_FAILURE(status)) {
+ return;
+ }
if (fResults == nullptr) {
status = fErrorCode;
return;
}
void FormattedNumber::populateFieldPositionIterator(FieldPositionIterator& iterator, UErrorCode& status) {
- if (U_FAILURE(status)) { return; }
+ if (U_FAILURE(status)) {
+ return;
+ }
if (fResults == nullptr) {
status = fErrorCode;
return;
}
void FormattedNumber::getDecimalQuantity(DecimalQuantity& output, UErrorCode& status) const {
- if (U_FAILURE(status)) { return; }
+ if (U_FAILURE(status)) {
+ return;
+ }
if (fResults == nullptr) {
status = fErrorCode;
return;
* 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 <em>field</em> in the output string.
* This allows you to determine the locations of the integer part, fraction part, and sign.
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() {