📄 decimalformat.java
字号:
/* DecimalFormat.java -- Formats and parses numbers Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version. GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package java.text;import java.util.Locale;import java.util.MissingResourceException;import java.util.ResourceBundle;import java.io.ObjectInputStream;import java.io.IOException;/** * @author Tom Tromey <tromey@cygnus.com> * @date March 4, 1999 *//* Written using "Java Class Libraries", 2nd edition, plus online * API docs for JDK 1.2 from http://www.javasoft.com. * Status: Believed complete and correct to 1.2. * Note however that the docs are very unclear about how format parsing * should work. No doubt there are problems here. */public class DecimalFormat extends NumberFormat{ // This is a helper for applyPatternWithSymbols. It reads a prefix // or a suffix. It can cause some side-effects. private final int scanFix (String pattern, int index, StringBuffer buf, String patChars, DecimalFormatSymbols syms, boolean is_suffix) { int len = pattern.length(); buf.setLength(0); boolean multiplierSet = false; while (index < len) { char c = pattern.charAt(index); if (c == '\'' && index + 1 < len && pattern.charAt(index + 1) == '\'') { buf.append(c); ++index; } else if (c == '\'' && index + 2 < len && pattern.charAt(index + 2) == '\'') { buf.append(pattern.charAt(index + 1)); index += 2; } else if (c == '\u00a4') { if (index + 1 < len && pattern.charAt(index + 1) == '\u00a4') { buf.append(syms.getInternationalCurrencySymbol()); ++index; } else buf.append(syms.getCurrencySymbol()); } else if (is_suffix && c == syms.getPercent()) { if (multiplierSet) throw new IllegalArgumentException ("multiplier already set " + "- index: " + index); multiplierSet = true; multiplier = 100; buf.append(c); } else if (is_suffix && c == syms.getPerMill()) { if (multiplierSet) throw new IllegalArgumentException ("multiplier already set " + "- index: " + index); multiplierSet = true; multiplier = 1000; buf.append(c); } else if (patChars.indexOf(c) != -1) { // This is a pattern character. break; } else buf.append(c); ++index; } return index; } // A helper which reads a number format. private final 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; // // Scan integer part. // 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; } // We can only side-effect when parsing the positive format. if (is_positive) { groupingUsed = saw_group; groupingSize = (byte) countSinceGroup; minimumIntegerDigits = zeroCount; } // Early termination. if (index == max || pattern.charAt(index) == syms.getGroupingSeparator()) { if (is_positive) decimalSeparatorAlwaysShown = false; return index; } if (pattern.charAt(index) == syms.getDecimalSeparator()) { ++index; // // Scan fractional part. // 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() && 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()) { // // Scan exponential format. // 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; } } return index; } // This helper function creates a string consisting of all the // characters which can appear in a pattern and must be quoted. private final String patternChars (DecimalFormatSymbols syms) { StringBuffer buf = new StringBuffer (); buf.append(syms.getDecimalSeparator()); buf.append(syms.getDigit()); buf.append(syms.getExponential()); buf.append(syms.getGroupingSeparator()); // Adding this one causes pattern application to fail. // Of course, omitting is causes toPattern to fail. // ... but we already have bugs there. FIXME. // buf.append(syms.getMinusSign()); buf.append(syms.getPatternSeparator()); buf.append(syms.getPercent()); buf.append(syms.getPerMill()); buf.append(syms.getZeroDigit()); buf.append('\u00a4'); return buf.toString(); } private final void applyPatternWithSymbols (String pattern, DecimalFormatSymbols syms) { // Initialize to the state the parser expects. negativePrefix = ""; negativeSuffix = ""; positivePrefix = ""; positiveSuffix = ""; decimalSeparatorAlwaysShown = false; groupingSize = 0; minExponentDigits = 0; multiplier = 1; useExponentialNotation = false; groupingUsed = false; maximumFractionDigits = 0; maximumIntegerDigits = 309; minimumFractionDigits = 0; minimumIntegerDigits = 1; StringBuffer buf = new StringBuffer (); String patChars = patternChars (syms); int max = pattern.length(); int index = scanFix (pattern, 0, buf, patChars, syms, false); positivePrefix = buf.toString(); index = scanFormat (pattern, index, patChars, syms, true); index = scanFix (pattern, index, buf, patChars, syms, true); positiveSuffix = buf.toString(); if (index == pattern.length()) { // No negative info. 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); negativePrefix = buf.toString(); // We parse the negative format for errors but we don't let // it side-effect this object. index = scanFormat (pattern, index, patChars, syms, false); index = scanFix (pattern, index, buf, patChars, syms, true); negativeSuffix = buf.toString(); if (index != pattern.length()) throw new IllegalArgumentException ("end of pattern expected " + "- index: " + index); } } public void applyLocalizedPattern (String pattern) { // JCL p. 638 claims this throws a ParseException but p. 629 // contradicts this. Empirical tests with patterns of "0,###.0" // and "#.#.#" corroborate the p. 629 statement that an // IllegalArgumentException is thrown. applyPatternWithSymbols (pattern, symbols); } public void applyPattern (String pattern) { // JCL p. 638 claims this throws a ParseException but p. 629 // contradicts this. Empirical tests with patterns of "0,###.0" // and "#.#.#" corroborate the p. 629 statement that an // IllegalArgumentException is thrown. 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 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); } public StringBuffer format (double number, StringBuffer dest, FieldPosition fieldPos) { // A very special case. if (Double.isNaN(number)) { dest.append(symbols.getNaN()); if (fieldPos != null && fieldPos.getField() == INTEGER_FIELD) { 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()); 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 = (long) (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 && count % groupingSize == 0) dest.insert(index, symbols.getGroupingSeparator()); dest.insert(index, (char) (symbols.getZeroDigit() + dig)); ++count; } 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); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -