parse(String source) 调用的是parse(String source, ParsePosition parsePosition) ,而该方法返回的是详细的具体类,以下是DecimalFormat.parse(String source, ParsePosition parsePosition)的源代码public Number More ...parse(String text, ParsePosition pos) { // special case NaN if (text.regionMatches(pos.index, symbols.getNaN(), 0, symbols.getNaN().length())) { pos.index = pos.index + symbols.getNaN().length(); return new Double(Double.NaN); } boolean[] status = new boolean[STATUS_LENGTH]; if (!subparse(text, pos, positivePrefix, negativePrefix, digitList, false, status)) { return null; } // special case INFINITY if (status[STATUS_INFINITE]) { if (status[STATUS_POSITIVE] == (multiplier >= 0)) { return new Double(Double.POSITIVE_INFINITY); } else { return new Double(Double.NEGATIVE_INFINITY); } } if (multiplier == 0) { if (digitList.isZero()) { return new Double(Double.NaN); } else if (status[STATUS_POSITIVE]) { return new Double(Double.POSITIVE_INFINITY); } else { return new Double(Double.NEGATIVE_INFINITY); } } if (isParseBigDecimal()) { BigDecimal bigDecimalResult = digitList.getBigDecimal(); if (multiplier != 1) { try { bigDecimalResult = bigDecimalResult.divide(getBigDecimalMultiplier()); } catch (ArithmeticException e) { // non-terminating decimal expansion bigDecimalResult = bigDecimalResult.divide(getBigDecimalMultiplier(), roundingMode); } } if (!status[STATUS_POSITIVE]) { bigDecimalResult = bigDecimalResult.negate(); } return bigDecimalResult; } else { boolean gotDouble = true; boolean gotLongMinimum = false; double doubleResult = 0.0; long longResult = 0; // Finally, have DigitList parse the digits into a value. if (digitList.fitsIntoLong(status[STATUS_POSITIVE], isParseIntegerOnly())) { gotDouble = false; longResult = digitList.getLong(); if (longResult < 0) { // got Long.MIN_VALUE gotLongMinimum = true; } } else { doubleResult = digitList.getDouble(); } // Divide by multiplier. We have to be careful here not to do // unneeded conversions between double and long. if (multiplier != 1) { if (gotDouble) { doubleResult /= multiplier; } else { // Avoid converting to double if we can if (longResult % multiplier == 0) { longResult /= multiplier; } else { doubleResult = ((double)longResult) / multiplier; gotDouble = true; } } } if (!status[STATUS_POSITIVE] && !gotLongMinimum) { doubleResult = -doubleResult; longResult = -longResult; } // At this point, if we divided the result by the multiplier, the // result may fit into a long. We check for this case and return // a long if possible. // We must do this AFTER applying the negative (if appropriate) // in order to handle the case of LONG_MIN; otherwise, if we do // this with a positive value -LONG_MIN, the double is > 0, but // the long is < 0. We also must retain a double in the case of // -0.0, which will compare as == to a long 0 cast to a double // (bug 4162852). if (multiplier != 1 && gotDouble) { longResult = (long)doubleResult; gotDouble = ((doubleResult != (double)longResult) || (doubleResult == 0.0 && 1/doubleResult < 0.0)) && !isParseIntegerOnly(); } return gotDouble ? (Number)new Double(doubleResult) : (Number)new Long(longResult); } }
// special case NaN
if (text.regionMatches(pos.index, symbols.getNaN(), 0, symbols.getNaN().length())) {
pos.index = pos.index + symbols.getNaN().length();
return new Double(Double.NaN);
} boolean[] status = new boolean[STATUS_LENGTH];
if (!subparse(text, pos, positivePrefix, negativePrefix, digitList, false, status)) {
return null;
} // special case INFINITY
if (status[STATUS_INFINITE]) {
if (status[STATUS_POSITIVE] == (multiplier >= 0)) {
return new Double(Double.POSITIVE_INFINITY);
} else {
return new Double(Double.NEGATIVE_INFINITY);
}
} if (multiplier == 0) {
if (digitList.isZero()) {
return new Double(Double.NaN);
} else if (status[STATUS_POSITIVE]) {
return new Double(Double.POSITIVE_INFINITY);
} else {
return new Double(Double.NEGATIVE_INFINITY);
}
} if (isParseBigDecimal()) {
BigDecimal bigDecimalResult = digitList.getBigDecimal(); if (multiplier != 1) {
try {
bigDecimalResult = bigDecimalResult.divide(getBigDecimalMultiplier());
}
catch (ArithmeticException e) { // non-terminating decimal expansion
bigDecimalResult = bigDecimalResult.divide(getBigDecimalMultiplier(), roundingMode);
}
} if (!status[STATUS_POSITIVE]) {
bigDecimalResult = bigDecimalResult.negate();
}
return bigDecimalResult;
} else {
boolean gotDouble = true;
boolean gotLongMinimum = false;
double doubleResult = 0.0;
long longResult = 0; // Finally, have DigitList parse the digits into a value.
if (digitList.fitsIntoLong(status[STATUS_POSITIVE], isParseIntegerOnly())) {
gotDouble = false;
longResult = digitList.getLong();
if (longResult < 0) { // got Long.MIN_VALUE
gotLongMinimum = true;
}
} else {
doubleResult = digitList.getDouble();
} // Divide by multiplier. We have to be careful here not to do
// unneeded conversions between double and long.
if (multiplier != 1) {
if (gotDouble) {
doubleResult /= multiplier;
} else {
// Avoid converting to double if we can
if (longResult % multiplier == 0) {
longResult /= multiplier;
} else {
doubleResult = ((double)longResult) / multiplier;
gotDouble = true;
}
}
} if (!status[STATUS_POSITIVE] && !gotLongMinimum) {
doubleResult = -doubleResult;
longResult = -longResult;
} // At this point, if we divided the result by the multiplier, the
// result may fit into a long. We check for this case and return
// a long if possible.
// We must do this AFTER applying the negative (if appropriate)
// in order to handle the case of LONG_MIN; otherwise, if we do
// this with a positive value -LONG_MIN, the double is > 0, but
// the long is < 0. We also must retain a double in the case of
// -0.0, which will compare as == to a long 0 cast to a double
// (bug 4162852).
if (multiplier != 1 && gotDouble) {
longResult = (long)doubleResult;
gotDouble = ((doubleResult != (double)longResult) ||
(doubleResult == 0.0 && 1/doubleResult < 0.0)) &&
!isParseIntegerOnly();
} return gotDouble ?
(Number)new Double(doubleResult) : (Number)new Long(longResult);
}
}