2010-08-22 114 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而不是拋出一個異常,如果該字符串不解析爲一個雙。

相關問題