decimalformat.java
来自「kaffe Java 解释器语言,源码,Java的子集系统,开放源代码」· Java 代码 · 共 1,163 行 · 第 1/3 页
JAVA
1,163 行
{ 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()); // FIXME. return hash; } public boolean isDecimalSeparatorAlwaysShown () { return decimalSeparatorAlwaysShown; } public Number parse (String str, ParsePosition pos) { // Our strategy is simple: copy the text into a buffer, // translating or omitting locale-specific information. Then // let Double or Long convert the number for us. boolean is_neg = false; int index = pos.getIndex(); StringBuffer buf = new StringBuffer (); // We have to check both prefixes, because one might be empty. We // want to pick the longest prefix that matches. 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) { // By checking this way, we preserve ambiguity in the case // where the negative format differs only in suffix. We // check this again later. 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; } // FIXME: handle Inf and NaN. // FIXME: do we have to respect minimum digits? // What about leading zeros? What about multiplier? int start_index = index; int max = str.length(); int last = index + maximumIntegerDigits; 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); // FIXME: what about grouping size? if (groupingUsed && c == symbols.getGroupingSeparator()) { if (last_group != -1 && groupingSize != 0 && (index - last_group) % groupingSize != 0) { pos.setErrorIndex(index); return null; } last_group = index; } else if (c >= zero && c <= zero + 9) { buf.append((char) (c - zero + '0')); exp_part = false; } 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.append('.'); int_part = false; } else if (c == symbols.getExponential()) { buf.append('E'); int_part = false; exp_part = true; } else if (exp_part && (c == '+' || c == '-' || c == symbols.getMinusSign())) { // For exponential notation. buf.append(c); } else break; } if (index == start_index) { // Didn't see any digits. pos.setErrorIndex(index); return null; } // Check the suffix. We must do this before converting the // buffer to a number to handle the case of a number which is // the most negative Long. 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; if (is_neg) buf.insert(0, '-'); String t = buf.toString(); Number result = null; try { result = new Long (t); } catch (NumberFormatException x1) { try { result = new Double (t); } catch (NumberFormatException x2) { } } if (result == null) { pos.setErrorIndex(index); return null; } pos.setIndex(index + suffix.length()); return result; } /** * Sets the <code>Currency</code> on the * <code>DecimalFormatSymbols</code> used, which also sets the * currency symbols on those symbols. */ 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 final 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 final String computePattern (DecimalFormatSymbols syms) { StringBuffer mainPattern = new StringBuffer (); // We have to at least emit a zero for the minimum number of // digits. Past that we need hash marks up to the grouping // separator (and one beyond). int total_digits = Math.max(minimumIntegerDigits, groupingUsed ? groupingSize + 1: 0); 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()); // Inserting the gropuing operator afterwards is easier. if (groupingUsed) mainPattern.insert(mainPattern.length() - groupingSize, syms.getGroupingSeparator()); // See if we need decimal info. 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); } // These names are fixed by the serialization spec. private boolean decimalSeparatorAlwaysShown; private byte groupingSize; private byte minExponentDigits; private int multiplier; private String negativePrefix; private String negativeSuffix; private String positivePrefix; private String positiveSuffix; 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; } } // The locale-independent pattern symbols happen to be the same as // the US symbols. private static final DecimalFormatSymbols nonLocalizedSymbols = new DecimalFormatSymbols (Locale.US);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?