From 9d9471e586de111e7fc199b99af8662326e1742b Mon Sep 17 00:00:00 2001 From: Shane Carr Date: Wed, 7 Feb 2018 06:26:44 +0000 Subject: [PATCH] ICU-13453 Fixing DecimalFormat getPositivePrefix backwards compatibility. X-SVN-Rev: 40847 --- .../com/ibm/icu/number/FormattedNumber.java | 26 +++++++++---------- .../icu/dev/test/format/NumberFormatTest.java | 17 ++++++++++++ 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/icu4j/main/classes/core/src/com/ibm/icu/number/FormattedNumber.java b/icu4j/main/classes/core/src/com/ibm/icu/number/FormattedNumber.java index cec506f639..1121f40254 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/number/FormattedNumber.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/number/FormattedNumber.java @@ -138,31 +138,29 @@ public class FormattedNumber { /** * @internal - * @deprecated This API is ICU internal only. + * @deprecated This API is ICU internal only. Use {@link #populateFieldPosition} or + * {@link #getFieldIterator} for similar functionality. */ @Deprecated public String getPrefix() { NumberStringBuilder temp = new NumberStringBuilder(); - int length = micros.modOuter.apply(temp, 0, 0); - length += micros.modMiddle.apply(temp, 0, length); - /* length += */ micros.modInner.apply(temp, 0, length); - int prefixLength = micros.modOuter.getPrefixLength() + micros.modMiddle.getPrefixLength() - + micros.modInner.getPrefixLength(); + // #13453: DecimalFormat wants the affixes from the pattern only (modMiddle). + micros.modMiddle.apply(temp, 0, 0); + int prefixLength = micros.modMiddle.getPrefixLength(); return temp.subSequence(0, prefixLength).toString(); } /** * @internal - * @deprecated This API is ICU internal only. + * @deprecated This API is ICU internal only. Use {@link #populateFieldPosition} or + * {@link #getFieldIterator} for similar functionality. */ @Deprecated public String getSuffix() { NumberStringBuilder temp = new NumberStringBuilder(); - int length = micros.modOuter.apply(temp, 0, 0); - length += micros.modMiddle.apply(temp, 0, length); - length += micros.modInner.apply(temp, 0, length); - int prefixLength = micros.modOuter.getPrefixLength() + micros.modMiddle.getPrefixLength() - + micros.modInner.getPrefixLength(); + // #13453: DecimalFormat wants the affixes from the pattern only (modMiddle). + int length = micros.modMiddle.apply(temp, 0, 0); + int prefixLength = micros.modMiddle.getPrefixLength(); return temp.subSequence(prefixLength, length).toString(); } @@ -206,7 +204,7 @@ public class FormattedNumber { // #equals() or #hashCode() on them directly. FormattedNumber _other = (FormattedNumber) other; return Arrays.equals(nsb.toCharArray(), _other.nsb.toCharArray()) - ^ Arrays.equals(nsb.toFieldArray(), _other.nsb.toFieldArray()) - ^ fq.toBigDecimal().equals(_other.fq.toBigDecimal()); + && Arrays.equals(nsb.toFieldArray(), _other.nsb.toFieldArray()) + && fq.toBigDecimal().equals(_other.fq.toBigDecimal()); } } \ No newline at end of file diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java index 092fd04028..f449d885ae 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java @@ -5331,6 +5331,23 @@ public class NumberFormatTest extends TestFmwk { assertEquals("Grouping should be off", false, df.isGroupingUsed()); } + @Test + public void Test13453_AffixContent() { + DecimalFormat df = (DecimalFormat) DecimalFormat.getScientificInstance(); + assertEquals("Scientific should NOT be included", "", df.getPositiveSuffix()); + + df = CompactDecimalFormat.getInstance(ULocale.ENGLISH, CompactDecimalFormat.CompactStyle.SHORT); + assertEquals("Compact should NOT be included", "", df.getPositiveSuffix()); + + df = (DecimalFormat) DecimalFormat.getInstance(NumberFormat.ISOCURRENCYSTYLE); + df.setCurrency(Currency.getInstance("GBP")); + assertEquals("ISO currency SHOULD be included", "GBP", df.getPositivePrefix()); + + df = (DecimalFormat) DecimalFormat.getInstance(NumberFormat.PLURALCURRENCYSTYLE); + df.setCurrency(Currency.getInstance("GBP")); + assertEquals("Plural name SHOULD be included", " British pounds", df.getPositiveSuffix()); + } + @Test public void Test11035_FormatCurrencyAmount() { double amount = 12345.67;