From 1aa5185a360ee1d015ecf2d273e38d130d81c0c7 Mon Sep 17 00:00:00 2001 From: Shane Carr Date: Wed, 18 Apr 2018 23:55:55 +0000 Subject: [PATCH] ICU-13700 Renaming new getter/setter to magnitudeScale. X-SVN-Rev: 41246 --- icu4c/source/i18n/decimfmt.cpp | 12 ++++---- icu4c/source/i18n/number_decimfmtprops.cpp | 4 +-- icu4c/source/i18n/number_decimfmtprops.h | 4 +-- icu4c/source/i18n/number_multiplier.h | 2 +- icu4c/source/i18n/unicode/decimfmt.h | 34 +++++++++++++++------- icu4c/source/i18n/unicode/unum.h | 2 ++ icu4c/source/test/intltest/dcfmapts.cpp | 4 +-- icu4c/source/test/intltest/numfmtst.cpp | 11 +++++++ icu4c/source/test/intltest/numfmtst.h | 1 + 9 files changed, 50 insertions(+), 24 deletions(-) diff --git a/icu4c/source/i18n/decimfmt.cpp b/icu4c/source/i18n/decimfmt.cpp index 43399d1a139..c7192dc4691 100644 --- a/icu4c/source/i18n/decimfmt.cpp +++ b/icu4c/source/i18n/decimfmt.cpp @@ -167,7 +167,7 @@ DecimalFormat::setAttribute(UNumberFormatAttribute attr, int32_t newValue, UErro break; case UNUM_SCALE: - setScale(newValue); + setMultiplierScale(newValue); break; case UNUM_GROUPING_SIZE: @@ -280,7 +280,7 @@ int32_t DecimalFormat::getAttribute(UNumberFormatAttribute attr, UErrorCode& sta return getMultiplier(); case UNUM_SCALE: - return getScale(); + return getMultiplierScale(); case UNUM_GROUPING_SIZE: return getGroupingSize(); @@ -667,12 +667,12 @@ void DecimalFormat::setMultiplier(int32_t multiplier) { refreshFormatterNoError(); } -int32_t DecimalFormat::getScale() const { - return fProperties->scaleMultiplier; +int32_t DecimalFormat::getMultiplierScale() const { + return fProperties->multiplierScale; } -void DecimalFormat::setScale(int32_t newValue) { - fProperties->scaleMultiplier = newValue; +void DecimalFormat::setMultiplierScale(int32_t newValue) { + fProperties->multiplierScale = newValue; refreshFormatterNoError(); } diff --git a/icu4c/source/i18n/number_decimfmtprops.cpp b/icu4c/source/i18n/number_decimfmtprops.cpp index a292197e42a..e56f955f543 100644 --- a/icu4c/source/i18n/number_decimfmtprops.cpp +++ b/icu4c/source/i18n/number_decimfmtprops.cpp @@ -37,6 +37,7 @@ void DecimalFormatProperties::clear() { minimumIntegerDigits = -1; minimumSignificantDigits = -1; multiplier = 1; + multiplierScale = 0; negativePrefix.setToBogus(); negativePrefixPattern.setToBogus(); negativeSuffix.setToBogus(); @@ -55,7 +56,6 @@ void DecimalFormatProperties::clear() { positiveSuffixPattern.setToBogus(); roundingIncrement = 0.0; roundingMode.nullify(); - scaleMultiplier = 0; secondaryGroupingSize = -1; signAlwaysShown = false; } @@ -83,6 +83,7 @@ bool DecimalFormatProperties::operator==(const DecimalFormatProperties &other) c eq = eq && minimumIntegerDigits == other.minimumIntegerDigits; eq = eq && minimumSignificantDigits == other.minimumSignificantDigits; eq = eq && multiplier == other.multiplier; + eq = eq && multiplierScale == other.multiplierScale; eq = eq && negativePrefix == other.negativePrefix; eq = eq && negativePrefixPattern == other.negativePrefixPattern; eq = eq && negativeSuffix == other.negativeSuffix; @@ -101,7 +102,6 @@ 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 067c148ac75..1c7d88f46ce 100644 --- a/icu4c/source/i18n/number_decimfmtprops.h +++ b/icu4c/source/i18n/number_decimfmtprops.h @@ -100,7 +100,7 @@ struct U_I18N_API DecimalFormatProperties { int32_t formatWidth; int32_t groupingSize; bool groupingUsed; - int32_t magnitudeMultiplier; + int32_t magnitudeMultiplier; // internal field like multiplierScale but separate to avoid conflict int32_t maximumFractionDigits; int32_t maximumIntegerDigits; int32_t maximumSignificantDigits; @@ -110,6 +110,7 @@ struct U_I18N_API DecimalFormatProperties { int32_t minimumIntegerDigits; int32_t minimumSignificantDigits; int32_t multiplier; + int32_t multiplierScale; // ICU4C-only UnicodeString negativePrefix; UnicodeString negativePrefixPattern; UnicodeString negativeSuffix; @@ -129,7 +130,6 @@ 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/number_multiplier.h b/icu4c/source/i18n/number_multiplier.h index b31be8ed617..94f582e6fc8 100644 --- a/icu4c/source/i18n/number_multiplier.h +++ b/icu4c/source/i18n/number_multiplier.h @@ -32,7 +32,7 @@ class MultiplierFormatHandler : public MicroPropsGenerator, public UMemory { /** Gets a Scale from a DecimalFormatProperties. In Java, defined in RoundingUtils.java */ static inline Scale scaleFromProperties(const DecimalFormatProperties& properties) { - int32_t magnitudeMultiplier = properties.magnitudeMultiplier + properties.scaleMultiplier; + int32_t magnitudeMultiplier = properties.magnitudeMultiplier + properties.multiplierScale; int32_t arbitraryMultiplier = properties.multiplier; if (magnitudeMultiplier != 0 && arbitraryMultiplier != 1) { return Scale::byDoubleAndPowerOfTen(arbitraryMultiplier, magnitudeMultiplier); diff --git a/icu4c/source/i18n/unicode/decimfmt.h b/icu4c/source/i18n/unicode/decimfmt.h index d59e50d06d0..0bee8ffb350 100644 --- a/icu4c/source/i18n/unicode/decimfmt.h +++ b/icu4c/source/i18n/unicode/decimfmt.h @@ -1301,7 +1301,7 @@ class U_I18N_API DecimalFormat : public NumberFormat { * @param value The new setting for whether to show plus sign on positive numbers * @internal Technical Preview */ - void setSignAlwaysShown(UBool value); + virtual void setSignAlwaysShown(UBool value); /** * Get the multiplier for use in percent, permill, etc. @@ -1309,6 +1309,8 @@ class U_I18N_API DecimalFormat : public NumberFormat { * (For Arabic, use arabic percent symbol). * For a permill, set the suffixes to have "\\u2031" and the multiplier to be 1000. * + * The number may also be multiplied by a power of ten; see getMultiplierScale(). + * * @return the multiplier for use in percent, permill, etc. * Examples: with 100, 1.23 -> "123", and "123" -> 1.23 * @stable ICU 2.0 @@ -1320,7 +1322,9 @@ 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. + * + * This method only supports integer multipliers. To multiply by a non-integer, pair this + * method with setMultiplierScale(). * * @param newValue the new value of the multiplier for use in percent, permill, etc. * Examples: with 100, 1.23 -> "123", and "123" -> 1.23 @@ -1332,22 +1336,30 @@ class U_I18N_API DecimalFormat : public NumberFormat { * 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 + * This method is analogous to UNUM_SCALE in getAttribute. + * + * @return the new value of the power-of-ten multiplier. * @draft ICU 62 */ - int32_t getScale(void) const; + int32_t getMultiplierScale(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. + * Sets a power of ten by which number should be multiplied before formatting, which + * can be combined with setMultiplier() to multiply by any arbitrary decimal value. * - * @param newValue the new value of the multiplier for use in percent, permill, etc. - * Examples: with 100, 1.23 -> "123", and "123" -> 1.23 + * For example, to multiply numbers by 0.5 before formatting, you can do: + * + *
+     * df.setMultiplier(5);
+     * df.setMultiplierScale(-1);
+     * 
+ * + * This method is analogous to UNUM_SCALE in setAttribute. + * + * @param newValue the new value of the power-of-ten multiplier. * @draft ICU 62 */ - virtual void setScale(int32_t newValue); + virtual void setMultiplierScale(int32_t newValue); /** * Get the rounding increment. diff --git a/icu4c/source/i18n/unicode/unum.h b/icu4c/source/i18n/unicode/unum.h index a8894a0052d..89d5e2fb5a8 100644 --- a/icu4c/source/i18n/unicode/unum.h +++ b/icu4c/source/i18n/unicode/unum.h @@ -1018,6 +1018,8 @@ typedef enum UNumberFormatAttribute { *

Example: setting the scale to 3, 123 formats as "123,000" *

Example: setting the scale to -4, 123 formats as "0.0123" * + * This setting is analogous to getMultiplierScale() and setMultiplierScale() in decimfmt.h. + * * @stable ICU 51 */ UNUM_SCALE = 21, #ifndef U_HIDE_INTERNAL_API diff --git a/icu4c/source/test/intltest/dcfmapts.cpp b/icu4c/source/test/intltest/dcfmapts.cpp index 96d297074f6..611015c1595 100644 --- a/icu4c/source/test/intltest/dcfmapts.cpp +++ b/icu4c/source/test/intltest/dcfmapts.cpp @@ -612,9 +612,9 @@ void IntlTestDecimalFormatAPI::TestScale() // Test both the attribute and the setter if (i % 2 == 0) { pat.setAttribute(UNUM_SCALE, testData[i].inputScale,status); - assertEquals("", testData[i].inputScale, pat.getScale()); + assertEquals("", testData[i].inputScale, pat.getMultiplierScale()); } else { - pat.setScale(testData[i].inputScale); + pat.setMultiplierScale(testData[i].inputScale); assertEquals("", testData[i].inputScale, pat.getAttribute(UNUM_SCALE, status)); } pat.format(testData[i].inputValue, resultStr); diff --git a/icu4c/source/test/intltest/numfmtst.cpp b/icu4c/source/test/intltest/numfmtst.cpp index 524a29361d2..89760cb12d0 100644 --- a/icu4c/source/test/intltest/numfmtst.cpp +++ b/icu4c/source/test/intltest/numfmtst.cpp @@ -656,6 +656,7 @@ void NumberFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &n TESTCASE_AUTO(Test11035_FormatCurrencyAmount); TESTCASE_AUTO(Test11318_DoubleConversion); TESTCASE_AUTO(TestParsePercentRegression); + TESTCASE_AUTO(TestMultiplierWithScale); TESTCASE_AUTO_END; } @@ -9061,4 +9062,14 @@ void NumberFormatTest::TestParsePercentRegression() { } } +void NumberFormatTest::TestMultiplierWithScale() { + IcuTestErrorCode status(*this, "TestMultiplierWithScale"); + + // Test magnitude combined with multiplier, as shown in API docs + DecimalFormat df("0", {"en", status}, status); + df.setMultiplier(5); + df.setMultiplierScale(-1); + expect2(df, 100, u"50"); // round-trip test +} + #endif /* #if !UCONFIG_NO_FORMATTING */ diff --git a/icu4c/source/test/intltest/numfmtst.h b/icu4c/source/test/intltest/numfmtst.h index e17b825d03f..a22b93e9eb5 100644 --- a/icu4c/source/test/intltest/numfmtst.h +++ b/icu4c/source/test/intltest/numfmtst.h @@ -223,6 +223,7 @@ class NumberFormatTest: public CalendarTimeZoneTest { void Test11035_FormatCurrencyAmount(); void Test11318_DoubleConversion(); void TestParsePercentRegression(); + void TestMultiplierWithScale(); private: UBool testFormattableAsUFormattable(const char *file, int line, Formattable &f); -- 2.40.0