久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何檢查字符串是否可解析為雙精度字符串?

How to check that a string is parseable to a double?(如何檢查字符串是否可解析為雙精度字符串?)
本文介紹了如何檢查字符串是否可解析為雙精度字符串?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

是否有一種本地方式(最好不實現(xiàn)自己的方法)來檢查字符串是否可以用 Double.parseDouble() 解析?

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

推薦答案

常見的方法是使用正則表達式檢查它,就像 Double.valueOf(String) 文檔.

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.

如果您不想這樣做,try catch 仍然是一種選擇.

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

JavaDoc 建議的正則表達式如下:

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
}

這篇關(guān)于如何檢查字符串是否可解析為雙精度字符串?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 欧美日韩中文在线 | 亚洲精品一区在线 | 天堂视频免费 | 国产中文区二幕区2012 | 老妇激情毛片免费 | 欧美精品二区 | 欧美日韩成人网 | 国产高清视频一区 | 成年人黄色小视频 | 精品国产1区2区3区 在线国产视频 | 精品视频一区二区三区在线观看 | 美女福利网站 | 中文字幕一区二区三 | 欧美亚洲网站 | 亚洲欧洲精品一区 | 国产视频中文字幕 | 成人精品在线观看 | 中文在线一区二区 | 日韩二区 | 国产中文字幕在线观看 | 狠狠色综合欧美激情 | av毛片| 日本在线免费视频 | 成人h动漫精品一区二区器材 | 国产成人免费视频网站视频社区 | 精品国模一区二区三区欧美 | 久久久久一区二区三区 | 国产三区视频在线观看 | 欧洲精品在线观看 | 国产成人网| 欧美亚洲国产一区二区三区 | 中文字幕一二三 | 特a毛片| 不卡在线视频 | 日韩精品在线看 | 4hu最新网址| a级在线免费观看 | 欧美99| 91精品国产一区二区三区 | 亚洲成人在线网 | 久久这里有精品 |