return f;
}
+/**
+ * Get the rounding mode.
+ * @return A rounding mode
+ */
+NumberFormat::ERoundingMode NumberFormat::getRoundingMode() const {
+ // Default value. ICU4J throws an exception and we can't change this API.
+ return NumberFormat::ERoundingMode::kRoundUnnecessary;
+}
+
+/**
+ * Set the rounding mode. This has no effect unless the rounding
+ * increment is greater than zero.
+ * @param roundingMode A rounding mode
+ */
+void NumberFormat::setRoundingMode(NumberFormat::ERoundingMode /*roundingMode*/) {
+ // No-op ICU4J throws an exception, and we can't change this API.
+}
+
U_NAMESPACE_END
#endif /* #if !UCONFIG_NO_FORMATTING */
*/
class U_I18N_API DecimalFormat: public NumberFormat {
public:
- /**
- * Rounding mode.
- * @stable ICU 2.4
- */
- enum ERoundingMode {
- kRoundCeiling, /**< Round towards positive infinity */
- kRoundFloor, /**< Round towards negative infinity */
- kRoundDown, /**< Round towards zero */
- kRoundUp, /**< Round away from zero */
- kRoundHalfEven, /**< Round towards the nearest integer, or
- towards the nearest even integer if equidistant */
- kRoundHalfDown, /**< Round towards the nearest integer, or
- towards zero if equidistant */
- kRoundHalfUp, /**< Round towards the nearest integer, or
- away from zero if equidistant */
- /**
- * Return U_FORMAT_INEXACT_ERROR if number does not format exactly.
- * @stable ICU 4.8
- */
- kRoundUnnecessary
- };
-
/**
* Pad position.
* @stable ICU 2.4
*/
class U_I18N_API NumberFormat : public Format {
public:
+ /**
+ * Rounding mode.
+ * @stable ICU 2.4
+ */
+ enum ERoundingMode {
+ kRoundCeiling, /**< Round towards positive infinity */
+ kRoundFloor, /**< Round towards negative infinity */
+ kRoundDown, /**< Round towards zero */
+ kRoundUp, /**< Round away from zero */
+ kRoundHalfEven, /**< Round towards the nearest integer, or
+ towards the nearest even integer if equidistant */
+ kRoundHalfDown, /**< Round towards the nearest integer, or
+ towards zero if equidistant */
+ kRoundHalfUp, /**< Round towards the nearest integer, or
+ away from zero if equidistant */
+ /**
+ * Return U_FORMAT_INEXACT_ERROR if number does not format exactly.
+ * @stable ICU 4.8
+ */
+ kRoundUnnecessary
+ };
+
/**
* Alignment Field constants used to construct a FieldPosition object.
* Signifies that the position of the integer part or fraction part of
*/
virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const;
+ /**
+ * Get the rounding mode. This will always return NumberFormat::ERoundingMode::kRoundUnnecessary
+ * if the subclass does not support rounding.
+ * @return A rounding mode
+ * @draft ICU 60
+ */
+ virtual ERoundingMode getRoundingMode(void) const;
+
+ /**
+ * Set the rounding mode. If a subclass does not support rounding, this will do nothing.
+ * @param roundingMode A rounding mode
+ * @draft ICU 60
+ */
+ virtual void setRoundingMode(ERoundingMode roundingMode);
+
public:
/**
*/
virtual void setContext(UDisplayContext value, UErrorCode& status);
-#ifndef U_HIDE_DRAFT_API
/**
* Get the rounding mode.
* @return A rounding mode
* @draft ICU 60
*/
virtual void setRoundingMode(DecimalFormat::ERoundingMode roundingMode);
-#endif /* U_HIDE_DRAFT_API */
public:
/**
// TODO: what should this return?
return nf->getMinimumFractionDigits();
}
+ else if (attr == UNUM_ROUNDING_MODE) {
+ return nf->getRoundingMode();
+ }
- // DecimalFormat specific attributes
+ // The remaining attributes are only supported for DecimalFormat
const DecimalFormat* df = dynamic_cast<const DecimalFormat*>(nf);
if (df != NULL) {
UErrorCode ignoredStatus = U_ZERO_ERROR;
return df->getAttribute(attr, ignoredStatus);
}
- // RuleBasedNumberFormat specific attributes
- const RuleBasedNumberFormat* rbnf = dynamic_cast<const RuleBasedNumberFormat*>(nf);
- if (rbnf != NULL) {
- if (attr == UNUM_ROUNDING_MODE) {
- return rbnf->getRoundingMode();
- }
- }
-
return -1;
}
nf->setMinimumFractionDigits(newValue);
return nf->setMaximumFractionDigits(newValue);
}
+ else if (attr == UNUM_ROUNDING_MODE) {
+ return nf->setRoundingMode((NumberFormat::ERoundingMode)newValue);
+ }
- // DecimalFormat specific attributes
+ // The remaining attributes are only supported for DecimalFormat
DecimalFormat* df = dynamic_cast<DecimalFormat*>(nf);
if (df != NULL) {
UErrorCode ignoredStatus = U_ZERO_ERROR;
df->setAttribute(attr, newValue, ignoredStatus);
}
-
- // RuleBasedNumberFormat specific attributes
- RuleBasedNumberFormat* rbnf = dynamic_cast<RuleBasedNumberFormat*>(nf);
- if (rbnf != NULL) {
- if (attr == UNUM_ROUNDING_MODE) {
- return rbnf->setRoundingMode((DecimalFormat::ERoundingMode)newValue);
- }
- }
}
U_CAPI double U_EXPORT2