DecimalFormat.java [plain text]
package java.text;
import gnu.java.text.AttributedFormatBuffer;
import gnu.java.text.FormatBuffer;
import gnu.java.text.FormatCharacterIterator;
import gnu.java.text.StringFormatBuffer;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Currency;
import java.util.HashMap;
import java.util.Locale;
public class DecimalFormat extends NumberFormat
{
private int scanFix (String pattern, int index, FormatBuffer buf,
String patChars, DecimalFormatSymbols syms,
boolean is_suffix)
{
int len = pattern.length();
boolean quoteStarted = false;
buf.clear();
boolean multiplierSet = false;
while (index < len)
{
char c = pattern.charAt(index);
if (quoteStarted)
{
if (c == '\'')
quoteStarted = false;
else
buf.append(c);
index++;
continue;
}
if (c == '\'' && index + 1 < len
&& pattern.charAt(index + 1) == '\'')
{
buf.append(c);
index++;
}
else if (c == '\'')
{
quoteStarted = true;
}
else if (c == '\u00a4')
{
if (index + 1 < len && pattern.charAt(index + 1) == '\u00a4')
{
buf.append(syms.getInternationalCurrencySymbol(), NumberFormat.Field.CURRENCY);
index++;
}
else
buf.append(syms.getCurrencySymbol(), NumberFormat.Field.CURRENCY);
}
else if (c == syms.getPercent())
{
if (multiplierSet)
throw new IllegalArgumentException ("multiplier already set " +
"- index: " + index);
multiplierSet = true;
multiplier = 100;
buf.append(c, NumberFormat.Field.PERCENT);
}
else if (c == syms.getPerMill())
{
if (multiplierSet)
throw new IllegalArgumentException ("multiplier already set " +
"- index: " + index);
multiplierSet = true;
multiplier = 1000;
buf.append(c, NumberFormat.Field.PERMILLE);
}
else if (patChars.indexOf(c) != -1)
{
break;
}
else
buf.append(c);
index++;
}
if (quoteStarted)
throw new IllegalArgumentException ("pattern is lacking a closing quote");
return index;
}
private int scanFormat (String pattern, int index, String patChars,
DecimalFormatSymbols syms, boolean is_positive)
{
int max = pattern.length();
int countSinceGroup = 0;
int zeroCount = 0;
boolean saw_group = false;
while (index < max)
{
char c = pattern.charAt(index);
if (c == syms.getDigit())
{
if (zeroCount > 0)
throw new IllegalArgumentException ("digit mark following " +
"zero - index: " + index);
++countSinceGroup;
}
else if (c == syms.getZeroDigit())
{
++zeroCount;
++countSinceGroup;
}
else if (c == syms.getGroupingSeparator())
{
countSinceGroup = 0;
saw_group = true;
}
else
break;
++index;
}
if (is_positive)
{
groupingUsed = saw_group;
groupingSize = (byte) countSinceGroup;
minimumIntegerDigits = zeroCount;
}
if (index == max || pattern.charAt(index) == syms.getGroupingSeparator())
{
if (is_positive)
decimalSeparatorAlwaysShown = false;
return index;
}
if (pattern.charAt(index) == syms.getDecimalSeparator())
{
++index;
int hashCount = 0;
zeroCount = 0;
while (index < max)
{
char c = pattern.charAt(index);
if (c == syms.getZeroDigit())
{
if (hashCount > 0)
throw new IllegalArgumentException ("zero mark " +
"following digit - index: " + index);
++zeroCount;
}
else if (c == syms.getDigit())
{
++hashCount;
}
else if (c != syms.getExponential()
&& c != syms.getPatternSeparator()
&& c != syms.getPercent()
&& c != syms.getPerMill()
&& patChars.indexOf(c) != -1)
throw new IllegalArgumentException ("unexpected special " +
"character - index: " + index);
else
break;
++index;
}
if (is_positive)
{
maximumFractionDigits = hashCount + zeroCount;
minimumFractionDigits = zeroCount;
}
if (index == max)
return index;
}
if (pattern.charAt(index) == syms.getExponential())
{
zeroCount = 0;
++index;
while (index < max)
{
char c = pattern.charAt(index);
if (c == syms.getZeroDigit())
++zeroCount;
else if (c == syms.getDigit())
{
if (zeroCount > 0)
throw new
IllegalArgumentException ("digit mark following zero " +
"in exponent - index: " +
index);
}
else if (patChars.indexOf(c) != -1)
throw new IllegalArgumentException ("unexpected special " +
"character - index: " +
index);
else
break;
++index;
}
if (is_positive)
{
useExponentialNotation = true;
minExponentDigits = (byte) zeroCount;
}
maximumIntegerDigits = groupingSize;
groupingSize = 0;
if (maximumIntegerDigits > minimumIntegerDigits && maximumIntegerDigits > 0)
{
minimumIntegerDigits = 1;
exponentRound = maximumIntegerDigits;
}
else
exponentRound = 1;
}
return index;
}
private String patternChars (DecimalFormatSymbols syms)
{
StringBuffer buf = new StringBuffer ();
buf.append(syms.getDecimalSeparator());
buf.append(syms.getDigit());
buf.append(syms.getExponential());
buf.append(syms.getGroupingSeparator());
buf.append(syms.getPatternSeparator());
buf.append(syms.getPercent());
buf.append(syms.getPerMill());
buf.append(syms.getZeroDigit());
buf.append('\u00a4');
return buf.toString();
}
private void applyPatternWithSymbols(String pattern, DecimalFormatSymbols syms)
{
negativePrefix = "";
negativeSuffix = "";
positivePrefix = "";
positiveSuffix = "";
decimalSeparatorAlwaysShown = false;
groupingSize = 0;
minExponentDigits = 0;
multiplier = 1;
useExponentialNotation = false;
groupingUsed = false;
maximumFractionDigits = 0;
maximumIntegerDigits = MAXIMUM_INTEGER_DIGITS;
minimumFractionDigits = 0;
minimumIntegerDigits = 1;
AttributedFormatBuffer buf = new AttributedFormatBuffer ();
String patChars = patternChars (syms);
int max = pattern.length();
int index = scanFix (pattern, 0, buf, patChars, syms, false);
buf.sync();
positivePrefix = buf.getBuffer().toString();
positivePrefixRanges = buf.getRanges();
positivePrefixAttrs = buf.getAttributes();
index = scanFormat (pattern, index, patChars, syms, true);
index = scanFix (pattern, index, buf, patChars, syms, true);
buf.sync();
positiveSuffix = buf.getBuffer().toString();
positiveSuffixRanges = buf.getRanges();
positiveSuffixAttrs = buf.getAttributes();
if (index == pattern.length())
{
negativePrefix = null;
negativeSuffix = null;
}
else
{
if (pattern.charAt(index) != syms.getPatternSeparator())
throw new IllegalArgumentException ("separator character " +
"expected - index: " + index);
index = scanFix (pattern, index + 1, buf, patChars, syms, false);
buf.sync();
negativePrefix = buf.getBuffer().toString();
negativePrefixRanges = buf.getRanges();
negativePrefixAttrs = buf.getAttributes();
index = scanFormat (pattern, index, patChars, syms, false);
index = scanFix (pattern, index, buf, patChars, syms, true);
buf.sync();
negativeSuffix = buf.getBuffer().toString();
negativeSuffixRanges = buf.getRanges();
negativeSuffixAttrs = buf.getAttributes();
if (index != pattern.length())
throw new IllegalArgumentException ("end of pattern expected " +
"- index: " + index);
}
}
public void applyLocalizedPattern (String pattern)
{
applyPatternWithSymbols (pattern, symbols);
}
public void applyPattern (String pattern)
{
applyPatternWithSymbols (pattern, nonLocalizedSymbols);
}
public Object clone ()
{
DecimalFormat c = (DecimalFormat) super.clone ();
c.symbols = (DecimalFormatSymbols) symbols.clone ();
return c;
}
public DecimalFormat ()
{
this ("#,##0.###");
}
public DecimalFormat (String pattern)
{
this (pattern, new DecimalFormatSymbols ());
}
public DecimalFormat (String pattern, DecimalFormatSymbols symbols)
{
this.symbols = symbols;
applyPattern (pattern);
}
private boolean equals(String s1, String s2)
{
if (s1 == null || s2 == null)
return s1 == s2;
return s1.equals(s2);
}
public boolean equals (Object obj)
{
if (! (obj instanceof DecimalFormat))
return false;
DecimalFormat dup = (DecimalFormat) obj;
return (decimalSeparatorAlwaysShown == dup.decimalSeparatorAlwaysShown
&& groupingSize == dup.groupingSize
&& minExponentDigits == dup.minExponentDigits
&& multiplier == dup.multiplier
&& equals(negativePrefix, dup.negativePrefix)
&& equals(negativeSuffix, dup.negativeSuffix)
&& equals(positivePrefix, dup.positivePrefix)
&& equals(positiveSuffix, dup.positiveSuffix)
&& symbols.equals(dup.symbols)
&& useExponentialNotation == dup.useExponentialNotation);
}
private void formatInternal (double number, FormatBuffer dest,
FieldPosition fieldPos)
{
if (Double.isNaN(number))
{
dest.append(symbols.getNaN());
if (fieldPos != null &&
(fieldPos.getField() == INTEGER_FIELD ||
fieldPos.getFieldAttribute() == NumberFormat.Field.INTEGER))
{
int index = dest.length();
fieldPos.setBeginIndex(index - symbols.getNaN().length());
fieldPos.setEndIndex(index);
}
return;
}
boolean is_neg = number < 0;
if (is_neg)
{
if (negativePrefix != null)
dest.append(negativePrefix, negativePrefixRanges, negativePrefixAttrs);
else
{
dest.append(symbols.getMinusSign(), NumberFormat.Field.SIGN);
dest.append(positivePrefix, positivePrefixRanges, positivePrefixAttrs);
}
number = - number;
}
else
dest.append(positivePrefix, positivePrefixRanges, positivePrefixAttrs);
int integerBeginIndex = dest.length();
int integerEndIndex = 0;
int zeroStart = symbols.getZeroDigit() - '0';
if (Double.isInfinite (number))
{
dest.append(symbols.getInfinity());
integerEndIndex = dest.length();
}
else
{
number *= multiplier;
long exponent = 0;
double baseNumber;
if (useExponentialNotation)
{
exponent = (long) Math.floor (Math.log(number) / Math.log(10));
exponent = exponent - (exponent % exponentRound);
if (minimumIntegerDigits > 0)
exponent -= minimumIntegerDigits - 1;
baseNumber = (number / Math.pow(10.0, exponent));
}
else
baseNumber = number;
baseNumber += 5 * Math.pow(10.0, - maximumFractionDigits - 1);
int index = dest.length();
String intPart = Long.toString((long)Math.floor(baseNumber));
int count, groupPosition = intPart.length();
dest.setDefaultAttribute(NumberFormat.Field.INTEGER);
for (count = 0; count < minimumIntegerDigits-intPart.length(); count++)
dest.append(symbols.getZeroDigit());
for (count = 0;
count < maximumIntegerDigits && count < intPart.length();
count++)
{
int dig = intPart.charAt(count);
if (groupingUsed && count > 0 && groupingSize != 0 && groupPosition % groupingSize == 0)
{
dest.append(symbols.getGroupingSeparator(), NumberFormat.Field.GROUPING_SEPARATOR);
dest.setDefaultAttribute(NumberFormat.Field.INTEGER);
}
dest.append((char) (zeroStart + dig));
groupPosition--;
}
dest.setDefaultAttribute(null);
integerEndIndex = dest.length();
int decimal_index = integerEndIndex;
int consecutive_zeros = 0;
int total_digits = 0;
int localMaximumFractionDigits = maximumFractionDigits;
if (useExponentialNotation)
localMaximumFractionDigits += minimumIntegerDigits - count;
double fracPart = baseNumber - Math.floor(baseNumber);
if ( ((fracPart != 0 || minimumFractionDigits > 0) && localMaximumFractionDigits > 0)
|| decimalSeparatorAlwaysShown)
{
dest.append (symbols.getDecimalSeparator(), NumberFormat.Field.DECIMAL_SEPARATOR);
}
int fraction_begin = dest.length();
dest.setDefaultAttribute(NumberFormat.Field.FRACTION);
for (count = 0;
count < localMaximumFractionDigits
&& (fracPart != 0 || count < minimumFractionDigits);
++count)
{
++total_digits;
fracPart *= 10;
long dig = (long) fracPart;
if (dig == 0)
++consecutive_zeros;
else
consecutive_zeros = 0;
dest.append((char) (symbols.getZeroDigit() + dig));
fracPart = fracPart - Math.floor (fracPart);
}
int extra_zeros = Math.min (consecutive_zeros,
total_digits - minimumFractionDigits);
if (extra_zeros > 0)
{
dest.cutTail(extra_zeros);
total_digits -= extra_zeros;
if (total_digits == 0 && !decimalSeparatorAlwaysShown)
dest.cutTail(1);
}
if (fieldPos != null && fieldPos.getField() == FRACTION_FIELD)
{
fieldPos.setBeginIndex(fraction_begin);
fieldPos.setEndIndex(dest.length());
}
if (useExponentialNotation)
{
dest.append(symbols.getExponential(), NumberFormat.Field.EXPONENT_SYMBOL);
if (exponent < 0)
{
dest.append (symbols.getMinusSign (), NumberFormat.Field.EXPONENT_SIGN);
exponent = - exponent;
}
index = dest.length();
dest.setDefaultAttribute(NumberFormat.Field.EXPONENT);
String exponentString = Long.toString ((long) exponent);
for (count = 0; count < minExponentDigits-exponentString.length();
count++)
dest.append((char) symbols.getZeroDigit());
for (count = 0;
count < exponentString.length();
++count)
{
int dig = exponentString.charAt(count);
dest.append((char) (zeroStart + dig));
}
}
}
if (fieldPos != null &&
(fieldPos.getField() == INTEGER_FIELD ||
fieldPos.getFieldAttribute() == NumberFormat.Field.INTEGER))
{
fieldPos.setBeginIndex(integerBeginIndex);
fieldPos.setEndIndex(integerEndIndex);
}
if (is_neg && negativeSuffix != null)
dest.append(negativeSuffix, negativeSuffixRanges, negativeSuffixAttrs);
else
dest.append(positiveSuffix, positiveSuffixRanges, positiveSuffixAttrs);
}
public StringBuffer format (double number, StringBuffer dest,
FieldPosition fieldPos)
{
formatInternal (number, new StringFormatBuffer(dest), fieldPos);
return dest;
}
public AttributedCharacterIterator formatToCharacterIterator (Object value)
{
AttributedFormatBuffer sbuf = new AttributedFormatBuffer();
if (value instanceof Number)
formatInternal(((Number) value).doubleValue(), sbuf, null);
else
throw new IllegalArgumentException
("Cannot format given Object as a Number");
sbuf.sync();
return new FormatCharacterIterator(sbuf.getBuffer().toString(),
sbuf.getRanges(),
sbuf.getAttributes());
}
public StringBuffer format (long number, StringBuffer dest,
FieldPosition fieldPos)
{
if (useExponentialNotation)
return format ((double) number, dest, fieldPos);
boolean is_neg = number < 0;
if (is_neg)
{
if (negativePrefix != null)
dest.append(negativePrefix);
else
{
dest.append(symbols.getMinusSign());
dest.append(positivePrefix);
}
number = - number;
}
else
dest.append(positivePrefix);
int integerBeginIndex = dest.length();
int index = dest.length();
int count = 0;
while (count < maximumIntegerDigits
&& (number > 0 || count < minimumIntegerDigits))
{
long dig = number % 10;
number /= 10;
if (dig < 0)
{
dig = - dig;
number = - number;
}
if (groupingUsed && count > 0 && groupingSize != 0 && count % groupingSize == 0)
dest.insert(index, symbols.getGroupingSeparator());
dest.insert(index, (char) (symbols.getZeroDigit() + dig));
++count;
}
if (fieldPos != null && fieldPos.getField() == INTEGER_FIELD)
{
fieldPos.setBeginIndex(integerBeginIndex);
fieldPos.setEndIndex(dest.length());
}
if (decimalSeparatorAlwaysShown || minimumFractionDigits > 0)
{
dest.append(symbols.getDecimalSeparator());
if (fieldPos != null && fieldPos.getField() == FRACTION_FIELD)
{
fieldPos.setBeginIndex(dest.length());
fieldPos.setEndIndex(dest.length() + minimumFractionDigits);
}
}
for (count = 0; count < minimumFractionDigits; ++count)
dest.append(symbols.getZeroDigit());
dest.append((is_neg && negativeSuffix != null)
? negativeSuffix
: positiveSuffix);
return dest;
}
public Currency getCurrency()
{
return symbols.getCurrency();
}
public DecimalFormatSymbols getDecimalFormatSymbols ()
{
return symbols;
}
public int getGroupingSize ()
{
return groupingSize;
}
public int getMultiplier ()
{
return multiplier;
}
public String getNegativePrefix ()
{
return negativePrefix;
}
public String getNegativeSuffix ()
{
return negativeSuffix;
}
public String getPositivePrefix ()
{
return positivePrefix;
}
public String getPositiveSuffix ()
{
return positiveSuffix;
}
public int hashCode ()
{
int hash = (negativeSuffix.hashCode() ^ negativePrefix.hashCode()
^positivePrefix.hashCode() ^ positiveSuffix.hashCode());
return hash;
}
public boolean isDecimalSeparatorAlwaysShown ()
{
return decimalSeparatorAlwaysShown;
}
public Number parse (String str, ParsePosition pos)
{
boolean is_neg = false;
int index = pos.getIndex();
StringBuffer int_buf = new StringBuffer ();
boolean got_pos = str.startsWith(positivePrefix, index);
String np = (negativePrefix != null
? negativePrefix
: positivePrefix + symbols.getMinusSign());
boolean got_neg = str.startsWith(np, index);
if (got_pos && got_neg)
{
if (np.length() > positivePrefix.length())
{
is_neg = true;
index += np.length();
}
else
index += positivePrefix.length();
}
else if (got_neg)
{
is_neg = true;
index += np.length();
}
else if (got_pos)
index += positivePrefix.length();
else
{
pos.setErrorIndex (index);
return null;
}
StringBuffer buf = int_buf;
StringBuffer frac_buf = null;
StringBuffer exp_buf = null;
int start_index = index;
int max = str.length();
int exp_index = -1;
int last = index + maximumIntegerDigits;
if (maximumFractionDigits > 0)
last += maximumFractionDigits + 1;
if (useExponentialNotation)
last += minExponentDigits + 1;
if (last > 0 && max > last)
max = last;
char zero = symbols.getZeroDigit();
int last_group = -1;
boolean int_part = true;
boolean exp_part = false;
for (; index < max; ++index)
{
char c = str.charAt(index);
if (groupingUsed && c == symbols.getGroupingSeparator())
{
if (last_group != -1
&& groupingSize != 0
&& (index - last_group) % groupingSize != 0)
{
pos.setErrorIndex(index);
return null;
}
last_group = index+1;
}
else if (c >= zero && c <= zero + 9)
{
buf.append((char) (c - zero + '0'));
}
else if (parseIntegerOnly)
break;
else if (c == symbols.getDecimalSeparator())
{
if (last_group != -1
&& groupingSize != 0
&& (index - last_group) % groupingSize != 0)
{
pos.setErrorIndex(index);
return null;
}
buf = frac_buf = new StringBuffer();
frac_buf.append('.');
int_part = false;
}
else if (c == symbols.getExponential())
{
buf = exp_buf = new StringBuffer();
int_part = false;
exp_part = true;
exp_index = index+1;
}
else if (exp_part
&& (c == '+' || c == '-' || c == symbols.getMinusSign()))
{
buf.append(c);
}
else
break;
}
if (index == start_index)
{
pos.setErrorIndex(index);
return null;
}
boolean got_pos_suf = str.startsWith(positiveSuffix, index);
String ns = (negativePrefix == null ? positiveSuffix : negativeSuffix);
boolean got_neg_suf = str.startsWith(ns, index);
if (is_neg)
{
if (! got_neg_suf)
{
pos.setErrorIndex(index);
return null;
}
}
else if (got_pos && got_neg && got_neg_suf)
{
is_neg = true;
}
else if (got_pos != got_pos_suf && got_neg != got_neg_suf)
{
pos.setErrorIndex(index);
return null;
}
String suffix = is_neg ? ns : positiveSuffix;
long multiplier = 1;
boolean use_long;
if (is_neg)
int_buf.insert(0, '-');
if (exp_buf != null)
{
int exponent_value;
try
{
exponent_value = Integer.parseInt(exp_buf.toString());
}
catch (NumberFormatException x1)
{
pos.setErrorIndex(exp_index);
return null;
}
if (frac_buf == null)
{
for (int i = 0; i < exponent_value; i++)
int_buf.append('0');
use_long = true;
}
else
{
boolean long_sufficient;
if (exponent_value < frac_buf.length()-1)
{
int lastNonNull = -1;
for (int i = 1; i < frac_buf.length(); i++)
if (frac_buf.charAt(i) != '0')
lastNonNull = i;
long_sufficient = (lastNonNull < 0 || lastNonNull <= exponent_value);
}
else
long_sufficient = true;
if (long_sufficient)
{
for (int i = 1; i < frac_buf.length() && i < exponent_value; i++)
int_buf.append(frac_buf.charAt(i));
for (int i = frac_buf.length()-1; i < exponent_value; i++)
int_buf.append('0');
use_long = true;
}
else
{
int_buf.append(frac_buf);
int_buf.append('E');
int_buf.append(exp_buf);
use_long = false;
}
}
}
else
{
if (frac_buf != null)
{
int i;
for (i = 1; i < frac_buf.length(); i++)
if (frac_buf.charAt(i) != '0')
break;
if (i != frac_buf.length())
{
use_long = false;
int_buf.append(frac_buf);
}
else
use_long = true;
}
else
use_long = true;
}
String t = int_buf.toString();
Number result = null;
if (use_long)
{
try
{
result = new Long (t);
}
catch (NumberFormatException x1)
{
}
}
else
{
try
{
result = new Double (t);
}
catch (NumberFormatException x2)
{
}
}
if (result == null)
{
pos.setErrorIndex(index);
return null;
}
pos.setIndex(index + suffix.length());
return result;
}
public void setCurrency(Currency currency)
{
symbols.setCurrency(currency);
}
public void setDecimalFormatSymbols (DecimalFormatSymbols newSymbols)
{
symbols = newSymbols;
}
public void setDecimalSeparatorAlwaysShown (boolean newValue)
{
decimalSeparatorAlwaysShown = newValue;
}
public void setGroupingSize (int groupSize)
{
groupingSize = (byte) groupSize;
}
public void setMaximumFractionDigits (int newValue)
{
super.setMaximumFractionDigits(Math.min(newValue, 340));
}
public void setMaximumIntegerDigits (int newValue)
{
super.setMaximumIntegerDigits(Math.min(newValue, 309));
}
public void setMinimumFractionDigits (int newValue)
{
super.setMinimumFractionDigits(Math.min(newValue, 340));
}
public void setMinimumIntegerDigits (int newValue)
{
super.setMinimumIntegerDigits(Math.min(newValue, 309));
}
public void setMultiplier (int newValue)
{
multiplier = newValue;
}
public void setNegativePrefix (String newValue)
{
negativePrefix = newValue;
}
public void setNegativeSuffix (String newValue)
{
negativeSuffix = newValue;
}
public void setPositivePrefix (String newValue)
{
positivePrefix = newValue;
}
public void setPositiveSuffix (String newValue)
{
positiveSuffix = newValue;
}
private void quoteFix(StringBuffer buf, String text, String patChars)
{
int len = text.length();
for (int index = 0; index < len; ++index)
{
char c = text.charAt(index);
if (patChars.indexOf(c) != -1)
{
buf.append('\'');
buf.append(c);
buf.append('\'');
}
else
buf.append(c);
}
}
private String computePattern(DecimalFormatSymbols syms)
{
StringBuffer mainPattern = new StringBuffer ();
int total_digits = Math.max(minimumIntegerDigits,
groupingUsed ? groupingSize + 1: groupingSize);
for (int i = 0; i < total_digits - minimumIntegerDigits; ++i)
mainPattern.append(syms.getDigit());
for (int i = total_digits - minimumIntegerDigits; i < total_digits; ++i)
mainPattern.append(syms.getZeroDigit());
if (groupingUsed)
mainPattern.insert(mainPattern.length() - groupingSize,
syms.getGroupingSeparator());
if (minimumFractionDigits > 0 || maximumFractionDigits > 0
|| decimalSeparatorAlwaysShown)
mainPattern.append(syms.getDecimalSeparator());
for (int i = 0; i < minimumFractionDigits; ++i)
mainPattern.append(syms.getZeroDigit());
for (int i = minimumFractionDigits; i < maximumFractionDigits; ++i)
mainPattern.append(syms.getDigit());
if (useExponentialNotation)
{
mainPattern.append(syms.getExponential());
for (int i = 0; i < minExponentDigits; ++i)
mainPattern.append(syms.getZeroDigit());
if (minExponentDigits == 0)
mainPattern.append(syms.getDigit());
}
String main = mainPattern.toString();
String patChars = patternChars (syms);
mainPattern.setLength(0);
quoteFix (mainPattern, positivePrefix, patChars);
mainPattern.append(main);
quoteFix (mainPattern, positiveSuffix, patChars);
if (negativePrefix != null)
{
quoteFix (mainPattern, negativePrefix, patChars);
mainPattern.append(main);
quoteFix (mainPattern, negativeSuffix, patChars);
}
return mainPattern.toString();
}
public String toLocalizedPattern ()
{
return computePattern (symbols);
}
public String toPattern ()
{
return computePattern (nonLocalizedSymbols);
}
private static final int MAXIMUM_INTEGER_DIGITS = 309;
private boolean decimalSeparatorAlwaysShown;
private byte groupingSize;
private byte minExponentDigits;
private int exponentRound;
private int multiplier;
private String negativePrefix;
private String negativeSuffix;
private String positivePrefix;
private String positiveSuffix;
private int[] negativePrefixRanges, positivePrefixRanges;
private HashMap[] negativePrefixAttrs, positivePrefixAttrs;
private int[] negativeSuffixRanges, positiveSuffixRanges;
private HashMap[] negativeSuffixAttrs, positiveSuffixAttrs;
private int serialVersionOnStream = 1;
private DecimalFormatSymbols symbols;
private boolean useExponentialNotation;
private static final long serialVersionUID = 864413376551465018L;
private void readObject(ObjectInputStream stream)
throws IOException, ClassNotFoundException
{
stream.defaultReadObject();
if (serialVersionOnStream < 1)
{
useExponentialNotation = false;
serialVersionOnStream = 1;
}
}
private static final DecimalFormatSymbols nonLocalizedSymbols
= new DecimalFormatSymbols (Locale.US);
}