【发布时间】:2010-08-22 22:32:30
【问题描述】:
是否有本地方法(最好不实现您自己的方法)来检查字符串是否可使用Double.parseDouble() 解析?
【问题讨论】:
标签: java string parsing floating-point
是否有本地方法(最好不实现您自己的方法)来检查字符串是否可使用Double.parseDouble() 解析?
【问题讨论】:
标签: java string parsing floating-point
Apache 像往常一样从Apache Commons-Lang 得到一个很好的答案,形式为
NumberUtils.isCreatable(String).
处理 nulls,不需要 try/catch 块。
【讨论】:
NumberUtils.isNumber(String) 已被弃用,您现在应该改用NumberUtils.isCreatable(String)。
您始终可以将 Double.parseDouble() 包装在 try catch 块中。
try
{
Double.parseDouble(number);
}
catch(NumberFormatException e)
{
//not a double
}
【讨论】:
number.trim().
常见的方法是使用正则表达式进行检查,就像在 Double.valueOf(String) 文档中所建议的那样。
那里提供的(或包含在下面的)正则表达式应该涵盖所有有效的浮点情况,所以你不需要摆弄它,因为你最终会错过一些更好的点。
如果您不想这样做,try catch 仍然是一个选择。
JavaDoc 建议的正则表达式如下:
final String Digits = "(\\p{Digit}+)";
final String HexDigits = "(\\p{XDigit}+)";
// an exponent is 'e' or 'E' followed by an optionally
// signed decimal integer.
final String Exp = "[eE][+-]?"+Digits;
final String fpRegex =
("[\\x00-\\x20]*"+ // Optional leading "whitespace"
"[+-]?(" + // Optional sign character
"NaN|" + // "NaN" string
"Infinity|" + // "Infinity" string
// A decimal floating-point string representing a finite positive
// number without a leading sign has at most five basic pieces:
// Digits . Digits ExponentPart FloatTypeSuffix
//
// Since this method allows integer-only strings as input
// in addition to strings of floating-point literals, the
// two sub-patterns below are simplifications of the grammar
// productions from the Java Language Specification, 2nd
// edition, section 3.10.2.
// Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
"((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+
// . Digits ExponentPart_opt FloatTypeSuffix_opt
"(\\.("+Digits+")("+Exp+")?)|"+
// Hexadecimal strings
"((" +
// 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt
"(0[xX]" + HexDigits + "(\\.)?)|" +
// 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt
"(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" +
")[pP][+-]?" + Digits + "))" +
"[fFdD]?))" +
"[\\x00-\\x20]*");// Optional trailing "whitespace"
if (Pattern.matches(fpRegex, myString)){
Double.valueOf(myString); // Will not throw NumberFormatException
} else {
// Perform suitable alternative action
}
【讨论】:
Double.parseDouble 的周围。但我会将 can 放入实用程序类或方法中,以便在您的项目中更容易重用。 parseDoubleSafe(String check, Double default) 之类的东西。
Double 类中自己提供该方法? :facepalm(无尽)
下面的内容就足够了:-
String decimalPattern = "([0-9]*)\\.([0-9]*)";
String number="20.00";
boolean match = Pattern.matches(decimalPattern, number);
System.out.println(match); //if true then decimal else not
【讨论】:
Google 的 Guava 库提供了一个很好的辅助方法:Doubles.tryParse(String)。您可以像 Double.parseDouble 一样使用它,但如果字符串未解析为双精度,它会返回 null 而不是抛出异常。
【讨论】:
所有答案都可以,这取决于您想成为多学术。 如果您希望准确地遵循 Java 规范,请使用以下内容:
private static final Pattern DOUBLE_PATTERN = Pattern.compile(
"[\\x00-\\x20]*[+-]?(NaN|Infinity|((((\\p{Digit}+)(\\.)?((\\p{Digit}+)?)" +
"([eE][+-]?(\\p{Digit}+))?)|(\\.((\\p{Digit}+))([eE][+-]?(\\p{Digit}+))?)|" +
"(((0[xX](\\p{XDigit}+)(\\.)?)|(0[xX](\\p{XDigit}+)?(\\.)(\\p{XDigit}+)))" +
"[pP][+-]?(\\p{Digit}+)))[fFdD]?))[\\x00-\\x20]*");
public static boolean isFloat(String s)
{
return DOUBLE_PATTERN.matcher(s).matches();
}
此代码基于Double 的 JavaDocs。
【讨论】: