]> granicus.if.org Git - icu/commitdiff
ICU-13453 Fixing DecimalFormat getPositivePrefix backwards compatibility.
authorShane Carr <shane@unicode.org>
Wed, 7 Feb 2018 06:26:44 +0000 (06:26 +0000)
committerShane Carr <shane@unicode.org>
Wed, 7 Feb 2018 06:26:44 +0000 (06:26 +0000)
X-SVN-Rev: 40847

icu4j/main/classes/core/src/com/ibm/icu/number/FormattedNumber.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java

index cec506f639707f6ea19958e7eb2b61dec637ce93..1121f402547a55d6019aa2018e578765515e9734 100644 (file)
@@ -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
index 092fd04028a47c9cce87a2af8b12fd8329a66d09..f449d885ae644e6cb1f26277a09586295139874c 100644 (file)
@@ -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;