如何检查一个字符串是可解析的双? [英] How to check that a string is parseable to a double?

查看:123
本文介绍了如何检查一个字符串是可解析的双?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一个本地的方式(最好是没有实现你自己的方法)来检查一个字符串是可以解析的 Double.parseDouble()

解决方案

通常的做法是用一个正则表达式来检查它,就像在 Double.valueOf(String) 的文档。

在那里提供的正则表达式应该包含所有有效的浮点数,所以你不需要弄乱它,因为你最终会错过一些更好的点。



如果你不想这样做, try catch

JavaDoc建议的正则表达式包含在下面:

pre > final String Digits =(\\p {Digit} +);
final String HexDigits =(\\p {XDigit} +);
//一个指数是'e'或者'E',后跟一个可选的
//带符号的十进制整数。
final String Exp =[eE] [+ - ]?+ Digits;
final String fpRegex =
([\\x00-\\x20] *+ //可选的前导空格
[+ - ]?(+ //可选符号字符
NaN |+ //NaN字符串
Infinity |+ //Infinity字符串

//十进制浮点字符串代表一个有限正数
//数字没有前导符号最多有五个基本部分:
//数字Digiter ExponentPart FloatTypeSuffix
//
//由于此方法允许整数字符串作为输入
//除了浮点文字字符串之外,
//下面的两个子模式是语法的简化
//从Java语言中产生规格,第二
版,第3.10.2节。

// Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
(((+ Digits +(\\。 )?(+ Digits +?)(+ Exp +)?)|+

// Digiter ExponentPart_opt FloatTypeSuffix_op (+ +)(+ Exp +)?)|+

//十六进制字符串

// 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] *); //可选结尾whitespace

if(Pattern.matches(fpRegex,myString)){
Double.valueOf(myString); //不会抛出NumberFormatException
} else {
//执行合适的替代动作
}


Is there a native way (preferably without implementing your own method) to check that a string is parseable with Double.parseDouble()?

解决方案

The common approach would be to check it with a regular expression like it's also suggested inside the Double.valueOf(String) documentation.

The regexp provided there (or included below) should cover all valid floating point cases, so you don't need to fiddle with it, since you will eventually miss out on some of the finer points.

If you don't want to do that, try catch is still an option.

The regexp suggested by the JavaDoc is included below:

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
}

这篇关于如何检查一个字符串是可解析的双?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆