From 02c492db8a46d7b9829b3a1afcc7d0fdd76c222d Mon Sep 17 00:00:00 2001 From: Shane Carr Date: Sat, 14 Apr 2018 09:49:12 +0000 Subject: [PATCH] ICU-13700 Adding DecimalFormat "scale" getter/setter and implementation in the new number formatting pipeline. X-SVN-Rev: 41232 --- icu4c/source/i18n/decimfmt.cpp | 18 ++++++++++++++++-- icu4c/source/i18n/number_decimfmtprops.cpp | 2 ++ icu4c/source/i18n/number_decimfmtprops.h | 1 + icu4c/source/i18n/unicode/decimfmt.h | 22 ++++++++++++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/icu4c/source/i18n/decimfmt.cpp b/icu4c/source/i18n/decimfmt.cpp index 8ca75eb10a9..f95b8112310 100644 --- a/icu4c/source/i18n/decimfmt.cpp +++ b/icu4c/source/i18n/decimfmt.cpp @@ -166,6 +166,10 @@ DecimalFormat::setAttribute(UNumberFormatAttribute attr, int32_t newValue, UErro setMultiplier(newValue); break; + case UNUM_SCALE: + setScale(newValue); + break; + case UNUM_GROUPING_SIZE: setGroupingSize(newValue); break; @@ -221,7 +225,6 @@ DecimalFormat::setAttribute(UNumberFormatAttribute attr, int32_t newValue, UErro status = U_UNSUPPORTED_ERROR; break; } - // TODO: UNUM_SCALE? // TODO: UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS? return *this; } @@ -273,6 +276,9 @@ int32_t DecimalFormat::getAttribute(UNumberFormatAttribute attr, UErrorCode& sta case UNUM_MULTIPLIER: return getMultiplier(); + case UNUM_SCALE: + return getScale(); + case UNUM_GROUPING_SIZE: return getGroupingSize(); @@ -311,7 +317,6 @@ int32_t DecimalFormat::getAttribute(UNumberFormatAttribute attr, UErrorCode& sta break; } // TODO: UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS? - // TODO: UNUM_SCALE? return -1; /* undefined */ } @@ -657,6 +662,15 @@ void DecimalFormat::setMultiplier(int32_t multiplier) { refreshFormatterNoError(); } +int32_t DecimalFormat::getScale() const { + return fProperties->scaleMultiplier; +} + +void DecimalFormat::setScale(int32_t newValue) { + fProperties->scaleMultiplier = newValue; + refreshFormatterNoError(); +} + double DecimalFormat::getRoundingIncrement(void) const { return fExportedProperties->roundingIncrement; } diff --git a/icu4c/source/i18n/number_decimfmtprops.cpp b/icu4c/source/i18n/number_decimfmtprops.cpp index f7d486d699e..412a04d8f35 100644 --- a/icu4c/source/i18n/number_decimfmtprops.cpp +++ b/icu4c/source/i18n/number_decimfmtprops.cpp @@ -54,6 +54,7 @@ void DecimalFormatProperties::clear() { positiveSuffixPattern.setToBogus(); roundingIncrement = 0.0; roundingMode.nullify(); + scaleMultiplier = 0; secondaryGroupingSize = -1; signAlwaysShown = false; } @@ -98,6 +99,7 @@ bool DecimalFormatProperties::operator==(const DecimalFormatProperties &other) c eq = eq && positiveSuffixPattern == other.positiveSuffixPattern; eq = eq && roundingIncrement == other.roundingIncrement; eq = eq && roundingMode == other.roundingMode; + eq = eq && scaleMultiplier == other.scaleMultiplier; eq = eq && secondaryGroupingSize == other.secondaryGroupingSize; eq = eq && signAlwaysShown == other.signAlwaysShown; return eq; diff --git a/icu4c/source/i18n/number_decimfmtprops.h b/icu4c/source/i18n/number_decimfmtprops.h index 4ad3225ba39..2cac011c7d6 100644 --- a/icu4c/source/i18n/number_decimfmtprops.h +++ b/icu4c/source/i18n/number_decimfmtprops.h @@ -128,6 +128,7 @@ struct U_I18N_API DecimalFormatProperties { UnicodeString positiveSuffixPattern; double roundingIncrement; NullableValue roundingMode; + int32_t scaleMultiplier; int32_t secondaryGroupingSize; bool signAlwaysShown; diff --git a/icu4c/source/i18n/unicode/decimfmt.h b/icu4c/source/i18n/unicode/decimfmt.h index ee6db91d1c5..1ce1ea93981 100644 --- a/icu4c/source/i18n/unicode/decimfmt.h +++ b/icu4c/source/i18n/unicode/decimfmt.h @@ -1320,6 +1320,7 @@ class U_I18N_API DecimalFormat : public NumberFormat { * For a percentage, set the suffixes to have "%" and the multiplier to be 100. * (For Arabic, use arabic percent symbol). * For a permill, set the suffixes to have "\\u2031" and the multiplier to be 1000. + * It is possible to set both via setMultiplier() as via setScale() simultaneously. * * @param newValue the new value of the multiplier for use in percent, permill, etc. * Examples: with 100, 1.23 -> "123", and "123" -> 1.23 @@ -1327,6 +1328,27 @@ class U_I18N_API DecimalFormat : public NumberFormat { */ virtual void setMultiplier(int32_t newValue); + /** + * Gets a multiplier for the given power of ten. + * For example, scale of 2 corresponds to a multiplier of 100. + * + * @return the multiplier for use in percent, permill, etc. + * Examples: with 100, 1.23 -> "123", and "123" -> 1.23 + * @draft ICU 62 + */ + int32_t getScale(void) const; + + /** + * Sets a multiplier for the given power of ten. + * For example, scale of 2 corresponds to a multiplier of 100. + * It is possible to set both via setMultiplier() as via setScale() simultaneously. + * + * @param newValue the new value of the multiplier for use in percent, permill, etc. + * Examples: with 100, 1.23 -> "123", and "123" -> 1.23 + * @draft ICU 62 + */ + virtual void setScale(int32_t newValue); + /** * Get the rounding increment. * @return A positive rounding increment, or 0.0 if a custom rounding -- 2.40.0