是否有一種本地方式(最好不實施自己的方法)檢查字符串是否可用Double.parseDouble()
解析?如何檢查字符串是否可解析爲雙精度?
回答
常見的方法是用正則表達式來檢查它喜歡它也是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. 檢查字符串是否爲雙精度
- 2. 檢查NSString是否爲雙精度型
- 3. 將字符串轉換爲雙精度,並檢查是否爲零
- 4. Casbah - 檢查字符串是否可解析爲ObjectId
- 5. Java:將字符串解析爲雙精度值
- 6. Java將字符串解析爲雙精度值
- 7. 在將C#中的字符串轉換爲雙精度數據後檢查輸入是否是字符串
- 8. 如何檢查cin的輸入是否爲雙精度?
- 9. 檢查是否可以解析字符串的最快方法
- 10. 從C中的字符串解析多個雙精度#
- 11. 解析和替換Java中的雙精度字符串
- 12. 解析字符串中的雙精度值
- 13. 從一個字符串解析多個雙精度
- 14. 將字符串轉換爲雙精度,
- 15. 將字符串轉換爲雙精度
- 16. 將字符串轉換爲雙精度?
- 17. 轉換精度雙爲字符串
- 18. 如何檢查用戶輸入是否是雙精度?
- 19. 檢測一個字符串是一個雙精度還是長精度C
- 20. 如何確定一個字符串類型是雙精度還是長精度
- 21. 如何解析NumberFormatException當解析字符串爲雙/整數
- 22. 如何檢查雙精度值是否有小數部分
- 23. 如何解析雙精度數組的數組的字符串表示形式?
- 24. 將字符串轉換爲雙精度 - 丟失精度
- 25. 檢查是否有可能將字符串分解爲塊?
- 26. 如何檢查字符串是否以精確的字符串結尾?
- 27. 將DBNULL值解析爲雙精度值
- 28. 如何解析字符串到雙
- 29. Java - 連續檢查輸入是否爲雙精度型
- 30. 檢查如果字符串是雙
+1 - 比捕捉異常要好得多(我的蹩腳答案...) – Starkey 2010-08-22 22:40:16
一個人怎麼做到這一點? – 2010-08-22 22:40:51
@Louis Rhys - 答案中的文檔鏈接中有一個代碼示例。 – Starkey 2010-08-22 22:44:22