如何在 Java 中将 Double 转换为 Number,如下面的代码所示?
public Number parse(String text, ParsePosition status) {
// find the best number (defined as the one with the longest parse)
int start = status.index;
int furthest = start;
double bestNumber = Double.NaN;
double tempNumber = 0.0;
for (int i = 0; i < choiceFormats.length; ++i) {
String tempString = choiceFormats[i];
if (Misc.regionMatches(start, tempString, 0, tempString.length(), text)) {
status.index = start + tempString.length();
tempNumber = choiceLimits[i];
if (status.index > furthest) {
furthest = status.index;
bestNumber = tempNumber;
if (furthest == text.length()) break;
}
}
}
status.index = furthest;
if (status.index == start) {
status.errorIndex = furthest;
}
int c= 0;
return new Double(bestNumber);
}
但在 Eclipse 中它显示
Type mismatch: cannot convert from Double to Number
实际上这段代码属于包中的ChoiceFormat.java
类java.text
。