如何检查字符串是否可解析为双精度?
常见的方法是用正则表达式来检查它喜欢它也是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
}
您可以始终在一个try catch块中包装Double.parseDouble()。
try
{
Double.parseDouble(number);
}
catch(NumberFormatException e)
{
//not a double
}
类似下面应该足够了: -
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
阿帕奇,像往常一样,拥有从Apache Commons-Lang的形式一个很好的答案 org.apache.commons.lang3.math.NumberUtils.isNumber(String)
把手零位,否try
/catch
需要的块。
Apache的JDK是否正常运行? – 2010-08-23 01:44:09
不可以。您可以在这里找到Apache Commons库。 http://commons.apache.org/强烈建议您使用它们,而不是随时编写自定义代码。 – 2010-08-23 02:14:58
这是否识别小数? – TechCrunch 2013-09-16 17:09:51
所有的答案都可以,这取决于你想成为什么样的学术。 如果你想准确地遵循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基于JavaDoc中。
谷歌的番石榴图书馆提供了一个很好的帮手:Doubles.tryParse(String)
。您使用它像Double.parseDouble
但它返回null
而不是抛出一个异常,如果该字符串不解析为一个双。
+1 - 比捕捉异常要好得多(我的蹩脚答案...) – Starkey 2010-08-22 22:40:16
一个人怎么做到这一点? – 2010-08-22 22:40:51
@Louis Rhys - 答案中的文档链接中有一个代码示例。 – Starkey 2010-08-22 22:44:22