From fa9f269f4d47497e9886ea638b45aa076ff1f082 Mon Sep 17 00:00:00 2001 From: Travis Keep Date: Wed, 28 Aug 2013 19:34:03 +0000 Subject: [PATCH] ICU-8474 CompactDecimalFormat improvement avoid extra format call. Fix bugs in FixedDecimal. X-SVN-Rev: 34100 --- .../ibm/icu/text/CompactDecimalFormat.java | 24 +++--------------- .../src/com/ibm/icu/text/DecimalFormat.java | 25 +++++++++++++------ .../src/com/ibm/icu/text/PluralRules.java | 4 +++ 3 files changed, 24 insertions(+), 29 deletions(-) diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/CompactDecimalFormat.java b/icu4j/main/classes/core/src/com/ibm/icu/text/CompactDecimalFormat.java index 777d24f811..600776e5cc 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/CompactDecimalFormat.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/CompactDecimalFormat.java @@ -334,7 +334,7 @@ public class CompactDecimalFormat extends DecimalFormat { base = CompactDecimalDataCache.MAX_DIGITS - 1; } number /= divisor[base]; - String pluralVariant = getPluralForm(number); + String pluralVariant = getPluralForm(getFixedDecimal(number, toDigitList(number))); if (pluralToCurrencyAffixes != null && currencyUnit != null) { currencyUnit.value = pluralToCurrencyAffixes.get(pluralVariant); } @@ -429,11 +429,11 @@ public class CompactDecimalFormat extends DecimalFormat { return result; } - private String getPluralForm(double number) { + private String getPluralForm(FixedDecimal fixedDecimal) { if (pluralRules == null) { return CompactDecimalDataCache.OTHER; } - return pluralRules.select(getNumberInfo(number)); + return pluralRules.select(fixedDecimal); } /** @@ -472,22 +472,4 @@ public class CompactDecimalFormat extends DecimalFormat { return unit; } } - - /** - * Return the NumberInfo for the number, given this formatter's settings. - * @param number The number. - * @return The NumberInfo for the given number. - * @internal - * @deprecated This API is ICU internal only. - */ - public FixedDecimal getNumberInfo(double number) { - if (getMaximumFractionDigits() == 0 && !areSignificantDigitsUsed()) { - return new FixedDecimal(number, 0, 0); - } - // TODO Fix hack, where we are formatting just to get the fraction digits - StringBuffer temp = new StringBuffer(); - UFieldPosition pos = new UFieldPosition(); - super.format(number, temp, pos); - return new FixedDecimal(number, pos.getCountVisibleFractionDigits(), pos.getFractionDigits()); - } } diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormat.java b/icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormat.java index e82b048324..7d0c1344f1 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormat.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormat.java @@ -917,9 +917,14 @@ public class DecimalFormat extends NumberFormat { if (Double.isInfinite(number)) { return number; } - DigitList dl = new DigitList(); - dl.set(number, precision(false), false); - return dl.getDouble(); + return toDigitList(number).getDouble(); + } + + @Deprecated + DigitList toDigitList(double number) { + DigitList result = new DigitList(); + result.set(number, precision(false), false); + return result; } /** @@ -1231,14 +1236,18 @@ public class DecimalFormat extends NumberFormat { */ /*package*/ FixedDecimal getFixedDecimal(double number) { // get the visible fractions and the number of fraction digits. - int fractionalDigitsInDigitList = digitList.count - digitList.decimalAt; + return getFixedDecimal(number, digitList); + } + + FixedDecimal getFixedDecimal(double number, DigitList dl) { + int fractionalDigitsInDigitList = dl.count - dl.decimalAt; int v; long f; int maxFractionalDigits; int minFractionalDigits; if (useSignificantDigits) { - maxFractionalDigits = maxSignificantDigits - digitList.decimalAt; - minFractionalDigits = minSignificantDigits - digitList.decimalAt; + maxFractionalDigits = maxSignificantDigits - dl.decimalAt; + minFractionalDigits = minSignificantDigits - dl.decimalAt; if (minFractionalDigits < 0) { minFractionalDigits = 0; } @@ -1257,9 +1266,9 @@ public class DecimalFormat extends NumberFormat { } f = 0; if (v > 0) { - for (int i = digitList.decimalAt; i < digitList.count; ++i) { + for (int i = Math.max(0, dl.decimalAt); i < dl.count; ++i) { f *= 10; - f += digitList.digits[i]; + f += (dl.digits[i] - '0'); } for (int i = v; i < fractionalDigitsInDigitList; ++i) { f *= 10; diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/PluralRules.java b/icu4j/main/classes/core/src/com/ibm/icu/text/PluralRules.java index df93295615..3516c75d6c 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/PluralRules.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/PluralRules.java @@ -494,6 +494,10 @@ public class PluralRules implements Serializable, Comparable { /** * @internal * @deprecated This API is ICU internal only. + * @param n is the original number + * @param v number of digits to the right of the decimal place. e.g 1.00 = 2 25. = 0 + * @param f Corresponds to f in the plural rules grammar. + * The digits to the right of the decimal place as an integer. e.g 1.10 = 10 */ public FixedDecimal(double n, int v, long f) { isNegative = n < 0;