From be31fc91967c4a8d6e62a36b303498563dabc67a Mon Sep 17 00:00:00 2001 From: Mario Torre Date: Fri, 1 Dec 2006 22:30:57 +0000 Subject: 2006-12-01 Mario Torre * java/text/DecimalFormat.java (formatInternal): move the formatting of fractional portion in a separate method. Also fixes the handling of decimal separator and its associated field. (handleFractionalPart): new method, needed to relax a bit formatInternal. --- ChangeLog | 8 +++ java/text/DecimalFormat.java | 162 +++++++++++++++++++++++++------------------ 2 files changed, 104 insertions(+), 66 deletions(-) diff --git a/ChangeLog b/ChangeLog index c3a24be03..2e1e969da 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2006-12-01 Mario Torre + + * java/text/DecimalFormat.java (formatInternal): move the formatting of + fractional portion in a separate method. + Also fixes the handling of decimal separator and its associated field. + (handleFractionalPart): new method, needed to relax a bit + formatInternal. + 2006-12-01 Andrew John Hughes * java/beans/beancontext/BeanContextServicesSupport.java: diff --git a/java/text/DecimalFormat.java b/java/text/DecimalFormat.java index 3b67a50da..b82d9e01b 100644 --- a/java/text/DecimalFormat.java +++ b/java/text/DecimalFormat.java @@ -1893,30 +1893,102 @@ public class DecimalFormat extends NumberFormat // add the INTEGER attribute addAttribute(Field.INTEGER, attributeStart, dest.length()); - if (this.decimalSeparatorAlwaysShown || - ((!isLong || this.useExponentialNotation) - && this.showDecimalSeparator && - this.maximumFractionDigits > 0) || - this.minimumFractionDigits > 0) + // ...update field position, if needed, and return... + if ((fieldPos.getField() == INTEGER_FIELD || + fieldPos.getFieldAttribute() == NumberFormat.Field.INTEGER)) + { + fieldPos.setBeginIndex(beginIndexInt); + fieldPos.setEndIndex(endIndexInt); + } + + handleFractionalPart(dest, fractPart, fieldPos, isLong); + + // and the exponent + if (this.useExponentialNotation) { attributeStart = dest.length(); + dest.append(symbols.getExponential()); + + addAttribute(Field.EXPONENT_SYMBOL, attributeStart, dest.length()); + attributeStart = dest.length(); + + if (exponent < 0) + { + dest.append(symbols.getMinusSign()); + exponent = -exponent; + + addAttribute(Field.EXPONENT_SIGN, attributeStart, dest.length()); + } + + attributeStart = dest.length(); + + String exponentString = String.valueOf(exponent); + int exponentLength = exponentString.length(); + + for (int i = 0; i < minExponentDigits - exponentLength; i++) + dest.append(symbols.getZeroDigit()); + + for (int i = 0; i < exponentLength; ++i) + dest.append(exponentString.charAt(i)); + + addAttribute(Field.EXPONENT, attributeStart, dest.length()); + } + + // now include the suffixes... + if (isNegative) + { + dest.append(negativeSuffix); + } + else + { + dest.append(positiveSuffix); + } + } + + /** + * Add to the input buffer the result of formatting the fractional + * portion of the number. + * + * @param dest + * @param fractPart + * @param fieldPos + * @param isLong + */ + private void handleFractionalPart(StringBuffer dest, String fractPart, + FieldPosition fieldPos, boolean isLong) + { + int dotStart = 0; + int dotEnd = 0; + boolean addDecimal = false; + + if (this.decimalSeparatorAlwaysShown || + ((!isLong || this.useExponentialNotation) && + this.showDecimalSeparator && this.maximumFractionDigits > 0) || + this.minimumFractionDigits > 0) + { + dotStart = dest.length(); + if (this.useCurrencySeparator) dest.append(symbols.getMonetaryDecimalSeparator()); else dest.append(symbols.getDecimalSeparator()); - // add the INTEGER attribute - addAttribute(Field.DECIMAL_SEPARATOR, attributeStart, dest.length()); + dotEnd = dest.length(); + addDecimal = true; } // now handle the fraction portion of the number + int fractStart = 0; + int fractEnd = 0; + boolean addFractional = false; + if ((!isLong || this.useExponentialNotation) && this.maximumFractionDigits > 0 || this.minimumFractionDigits > 0) { - attributeStart = dest.length(); - beginIndexFract = attributeStart; + fractStart = dest.length(); + fractEnd = fractStart; int digits = this.minimumFractionDigits; @@ -1939,83 +2011,41 @@ public class DecimalFormat extends NumberFormat if (fracts[i] != '0') allZeros = false; } - + if (!allZeros || (minimumFractionDigits > 0)) { appendDigit(fractPart, dest, false); - endIndexFract = dest.length(); - addAttribute(Field.FRACTION, attributeStart, endIndexFract); + fractEnd = dest.length(); + + addDecimal = true; + addFractional = true; } else if (!this.decimalSeparatorAlwaysShown) { dest.deleteCharAt(dest.length() - 1); + addDecimal = false; } else { - System.out.println("ayeeeee!"); - endIndexFract = dest.length(); - addAttribute(Field.FRACTION, attributeStart, endIndexFract); + fractEnd = dest.length(); + addFractional = true; } } - // and the exponent - if (this.useExponentialNotation) - { - attributeStart = dest.length(); - - dest.append(symbols.getExponential()); - - addAttribute(Field.EXPONENT_SYMBOL, attributeStart, dest.length()); - attributeStart = dest.length(); - - if (exponent < 0) - { - dest.append(symbols.getMinusSign()); - exponent = -exponent; - - addAttribute(Field.EXPONENT_SIGN, attributeStart, dest.length()); - } - - attributeStart = dest.length(); - - String exponentString = String.valueOf(exponent); - int exponentLength = exponentString.length(); - - for (int i = 0; i < minExponentDigits - exponentLength; i++) - dest.append(symbols.getZeroDigit()); - - for (int i = 0; i < exponentLength; ++i) - dest.append(exponentString.charAt(i)); - - addAttribute(Field.EXPONENT, attributeStart, dest.length()); - } - - // now include the suffixes... - if (isNegative) - { - dest.append(negativeSuffix); - } - else - { - dest.append(positiveSuffix); - } + if (addDecimal) + addAttribute(Field.DECIMAL_SEPARATOR, dotStart, dotEnd); - // ...update field position, if needed, and return... - if ((fieldPos.getField() == INTEGER_FIELD || - fieldPos.getFieldAttribute() == NumberFormat.Field.INTEGER)) - { - fieldPos.setBeginIndex(beginIndexInt); - fieldPos.setEndIndex(endIndexInt); - } + if (addFractional) + addAttribute(Field.FRACTION, fractStart, fractEnd); if ((fieldPos.getField() == FRACTION_FIELD || fieldPos.getFieldAttribute() == NumberFormat.Field.FRACTION)) { - fieldPos.setBeginIndex(beginIndexFract); - fieldPos.setEndIndex(endIndexFract); + fieldPos.setBeginIndex(fractStart); + fieldPos.setEndIndex(fractEnd); } } - + /** * Append to destthe give number of zeros. * Grouping is added if needed. -- cgit v1.2.1