Lou*_*hys 66 java string floating-point parsing
是否有一种本机方式(最好不要实现自己的方法)来检查字符串是否可解析Double.parseDouble()
?
blu*_*l2k 56
像往常一样,Apache以Apache Commons-Lang的形式
提供了很好的答案Apache
处理空值,不需要Apache Commons-Lang
/ NumberUtils.isCreatable(String)
阻止.
jdc*_*589 53
您始终可以在try catch块中包装Double.parseDouble().
try
{
Double.parseDouble(number);
}
catch(NumberFormatException e)
{
//not a double
}
Run Code Online (Sandbox Code Playgroud)
Joh*_*ter 45
常见的方法是使用正则表达式检查它,就像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
}
Run Code Online (Sandbox Code Playgroud)
像下面这样的东西就足够了: -
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
Run Code Online (Sandbox Code Playgroud)
谷歌的番石榴库提供了一个很好的辅助方法来做到这一点:Doubles.tryParse(String)
.您可以使用它,Double.parseDouble
但null
如果字符串不解析为double,则返回而不是抛出异常.
所有答案都可以,取决于你想要的学术水平.如果您希望准确地遵循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();
}
Run Code Online (Sandbox Code Playgroud)
此代码基于Double的JavaDocs .
归档时间: |
|
查看次数: |
125511 次 |
最近记录: |