decimalformat.java
来自「kaffe Java 解释器语言,源码,Java的子集系统,开放源代码」· Java 代码 · 共 1,163 行 · 第 1/3 页
JAVA
1,163 行
private final 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); } /** * This method is a helper function for formatters. Given a set of ranges * and attributes it adds exactly one attribute for the range of characters * comprised between the last entry in 'ranges' and the specified new range. * * @param ranges Vector containing a list of previously entered ranges for attributes. * @param attributes Vector containing a list of previously entered attributes * @param new_range A new range to insert in the list. * @param new_attribute A new attribute to insert in the list. */ private final void addAttribute(Vector ranges, Vector attributes, int new_range, Format.Field new_attribute) { HashMap map; if (new_attribute != null) { map = new HashMap(); map.put(new_attribute, new_attribute); attributes.add(map); } else attributes.add(null); ranges.add(new Integer(new_range)); } protected StringBuffer formatInternal (double number, StringBuffer dest, FieldPosition fieldPos, Vector ranges, Vector attributes) { boolean set_attributes = (ranges != null && attributes != null); // A very special case. 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 dest; } boolean is_neg = number < 0; if (is_neg) { if (negativePrefix != null) dest.append(negativePrefix); else { dest.append(symbols.getMinusSign()); if (set_attributes) { addAttribute(ranges, attributes, dest.length(), NumberFormat.Field.SIGN); addAttribute(ranges, attributes, dest.length()+positivePrefix.length(), null); } dest.append(positivePrefix); } number = - number; } else dest.append(positivePrefix); int integerBeginIndex = dest.length(); int integerEndIndex = 0; if (Double.isInfinite (number)) { dest.append(symbols.getInfinity()); integerEndIndex = dest.length(); } else { number *= multiplier; // Compute exponent. long exponent = 0; double baseNumber; if (useExponentialNotation) { exponent = (long) Math.floor (Math.log(number) / Math.log(10)); if (minimumIntegerDigits > 0) exponent -= minimumIntegerDigits - 1; baseNumber = (number / Math.pow(10.0, exponent)); } else baseNumber = number; // Round to the correct number of digits. baseNumber += 5 * Math.pow(10.0, - maximumFractionDigits - 1); int index = dest.length(); double intPart = Math.floor(baseNumber); int count = 0; while (count < maximumIntegerDigits && (intPart > 0 || count < minimumIntegerDigits)) { long dig = (long) (intPart % 10); intPart = Math.floor(intPart / 10); // Append group separator if required. if (groupingUsed && count > 0 && groupingSize != 0 && count % groupingSize == 0) { if (set_attributes) { addAttribute(ranges, attributes, dest.length(), NumberFormat.Field.INTEGER); addAttribute(ranges, attributes, dest.length()+1, NumberFormat.Field.GROUPING_SEPARATOR); } dest.insert(index, symbols.getGroupingSeparator()); } dest.insert(index, (char) (symbols.getZeroDigit() + dig)); ++count; } if (set_attributes) addAttribute(ranges, attributes, dest.length(), NumberFormat.Field.INTEGER); integerEndIndex = dest.length(); int decimal_index = integerEndIndex; int consecutive_zeros = 0; int total_digits = 0; // Strip integer part from NUMBER. double fracPart = baseNumber - Math.floor(baseNumber); for (count = 0; count < maximumFractionDigits && (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)); // Strip integer part from FRACPART. fracPart = fracPart - Math.floor (fracPart); } // Strip extraneous trailing `0's. We can't always detect // these in the loop. int extra_zeros = Math.min (consecutive_zeros, total_digits - minimumFractionDigits); if (extra_zeros > 0) { dest.setLength(dest.length() - extra_zeros); total_digits -= extra_zeros; } // If required, add the decimal symbol. if (decimalSeparatorAlwaysShown || total_digits > 0) { dest.insert(decimal_index, symbols.getDecimalSeparator()); if (set_attributes) { addAttribute(ranges, attributes, decimal_index + 1, NumberFormat.Field.DECIMAL_SEPARATOR); addAttribute(ranges, attributes, dest.length(), NumberFormat.Field.FRACTION); } if (fieldPos != null && (fieldPos.getField() == FRACTION_FIELD || fieldPos.getFieldAttribute() == NumberFormat.Field.FRACTION)) { fieldPos.setBeginIndex(decimal_index + 1); fieldPos.setEndIndex(dest.length()); } } // Finally, print the exponent. if (useExponentialNotation) { dest.append(symbols.getExponential()); if (set_attributes) addAttribute(ranges, attributes, dest.length(), NumberFormat.Field.EXPONENT_SYMBOL); if (exponent < 0) { dest.append (symbols.getMinusSign ()); exponent = - exponent; if (set_attributes) addAttribute(ranges, attributes, dest.length(), NumberFormat.Field.EXPONENT_SIGN); } index = dest.length(); for (count = 0; exponent > 0 || count < minExponentDigits; ++count) { long dig = exponent % 10; exponent /= 10; dest.insert(index, (char) (symbols.getZeroDigit() + dig)); } if (set_attributes) addAttribute(ranges, attributes, dest.length(), NumberFormat.Field.EXPONENT); } } if (fieldPos != null && (fieldPos.getField() == INTEGER_FIELD || fieldPos.getFieldAttribute() == NumberFormat.Field.INTEGER)) { fieldPos.setBeginIndex(integerBeginIndex); fieldPos.setEndIndex(integerEndIndex); } dest.append((is_neg && negativeSuffix != null) ? negativeSuffix : positiveSuffix); return dest; } public StringBuffer format (double number, StringBuffer dest, FieldPosition fieldPos) { return formatInternal (number, dest, fieldPos, null, null); } public AttributedCharacterIterator formatToCharacterIterator (Object value) { Vector ranges = new Vector(); Vector attributes = new Vector(); StringBuffer sbuf = new StringBuffer(); if (value instanceof Number) formatInternal(((Number) value).doubleValue(), sbuf, null, ranges, attributes); else throw new IllegalArgumentException ("Cannot format given Object as a Number"); int[] int_ranges = new int[ranges.size()]; HashMap[] array_attributes = new HashMap[ranges.size()]; for (int i=0;i<int_ranges.length;i++) int_ranges[i] = ((Integer)ranges.elementAt(i)).intValue(); System.arraycopy(attributes.toArray(), 0, array_attributes, 0, array_attributes.length); /* * TODO: Reparse buffer to find the position of currency, percent, permille. * As current classpath implementation preformat prefixes and suffixes we are * not able to place attributes just when symbols arrive. */ return new FormatCharacterIterator (sbuf.toString(), int_ranges, array_attributes); } public StringBuffer format (long number, StringBuffer dest, FieldPosition fieldPos) { // If using exponential notation, we just format as a double. 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; // NUMBER and DIG will be less than 0 if the original number // was the most negative long. if (dig < 0) { dig = - dig; number = - number; } // Append group separator if required. 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; } /** * Returns the currency corresponding to the currency symbol stored * in the instance of <code>DecimalFormatSymbols</code> used by this * <code>DecimalFormat</code>. * * @return A new instance of <code>Currency</code> if * the currency code matches a known one, null otherwise. */ public Currency getCurrency() { return symbols.getCurrency(); } public DecimalFormatSymbols getDecimalFormatSymbols () { return symbols; } public int getGroupingSize () { return groupingSize; } public int getMultiplier ()
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?