ICU-8474 CompactDecimalFormat improvement avoid extra format call. Fix bugs in FixedDecimal.
X-SVN-Rev: 34100
This commit is contained in:
parent
6037d39183
commit
fa9f269f4d
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -494,6 +494,10 @@ public class PluralRules implements Serializable, Comparable<PluralRules> {
|
||||
/**
|
||||
* @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;
|
||||
|
Loading…
Reference in New Issue
Block a user