2010-08-22 85 views

回答

36

常见的方法是用正则表达式来检查它喜欢它也是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 
} 
+0

+1 - 比捕捉异常要好得多(我的蹩脚答案...) – Starkey 2010-08-22 22:40:16

+0

一个人怎么做到这一点? – 2010-08-22 22:40:51

+2

@Louis Rhys - 答案中的文档链接中有一个代码示例。 – Starkey 2010-08-22 22:44:22

43

您可以始终在一个try catch块中包装Double.parseDouble()。

try 
{ 
    Double.parseDouble(number); 
} 
catch(NumberFormatException e) 
{ 
    //not a double 
} 
+7

这将是我的首选方法,因为您保证获得与解析完全相同的行为。自定义正则表达式很容易忽略边缘情况。 – 2010-08-23 02:17:12

+2

这是很好的,除非你输入一个数字后跟一个空格,否则它不能捕获它。 – giant91 2013-09-06 05:03:05

+2

@ giant91 - 修剪数字:'number.trim()'。 – 26hmkk 2016-07-15 15:24:48

8

类似下面应该足够了: -

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 
+2

实际上它比这更复杂。看到j​​ohannes wachter的回答 – 2010-08-22 22:47:29

+1

明白了。 Wachter提供的链接确实有帮助。 – CoolBeans 2010-08-23 13:56:31

+0

嗨,这是更适合双打:String doublePattern =“([0-9]。*)\\。([0-9]。*)”; – voodoo98 2015-05-07 09:32:38

48

阿帕奇,像往常一样,拥有从Apache Commons-Lang的形式一个很好的答案 org.apache.commons.lang3.math.NumberUtils.isNumber(String)

把手零位,否try/catch需要的块。

+0

Apache的JDK是否正常运行? – 2010-08-23 01:44:09

+5

不可以。您可以在这里找到Apache Commons库。 http://commons.apache.org/强烈建议您使用它们,而不是随时编写自定义代码。 – 2010-08-23 02:14:58

+1

这是否识别小数? – TechCrunch 2013-09-16 17:09:51

6

所有的答案都可以,这取决于你想成为什么样的学术。 如果你想准确地遵循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中。

6

谷歌的番石榴图书馆提供了一个很好的帮手:Doubles.tryParse(String)。您使用它像Double.parseDouble但它返回null而不是抛出一个异常,如果该字符串不解析为一个双。