+UnicodeString&
+RuleBasedNumberFormat::format(const DigitList &number,
+ UnicodeString &appendTo,
+ FieldPositionIterator *posIter,
+ UErrorCode &status) const {
+ if (U_FAILURE(status)) {
+ return appendTo;
+ }
+ DigitList copy(number);
+ if (copy.fitsIntoInt64(false)) {
+ format(((DigitList &)number).getInt64(), appendTo, posIter, status);
+ }
+ else {
+ copy.roundAtExponent(0);
+ if (copy.fitsIntoInt64(false)) {
+ format(number.getDouble(), appendTo, posIter, status);
+ }
+ else {
+ // We're outside of our normal range that this framework can handle.
+ // The DecimalFormat will provide more accurate results.
+
+ // TODO this section should probably be optimized. The DecimalFormat is shared in ICU4J.
+ NumberFormat *decimalFormat = NumberFormat::createInstance(locale, UNUM_DECIMAL, status);
+ Formattable f;
+ f.adoptDigitList(new DigitList(number));
+ decimalFormat->format(f, appendTo, posIter, status);
+ delete decimalFormat;
+ }
+ }
+ return appendTo;
+}
+
+
+UnicodeString&
+RuleBasedNumberFormat::format(const DigitList &number,
+ UnicodeString& appendTo,
+ FieldPosition& pos,
+ UErrorCode &status) const {
+ if (U_FAILURE(status)) {
+ return appendTo;
+ }
+ DigitList copy(number);
+ if (copy.fitsIntoInt64(false)) {
+ format(((DigitList &)number).getInt64(), appendTo, pos, status);
+ }
+ else {
+ copy.roundAtExponent(0);
+ if (copy.fitsIntoInt64(false)) {
+ format(number.getDouble(), appendTo, pos, status);
+ }
+ else {
+ // We're outside of our normal range that this framework can handle.
+ // The DecimalFormat will provide more accurate results.
+
+ // TODO this section should probably be optimized. The DecimalFormat is shared in ICU4J.
+ NumberFormat *decimalFormat = NumberFormat::createInstance(locale, UNUM_DECIMAL, status);
+ Formattable f;
+ f.adoptDigitList(new DigitList(number));
+ decimalFormat->format(f, appendTo, pos, status);
+ delete decimalFormat;
+ }
+ }
+ return appendTo;
+}
+