How to check that a string is being processed double?

Is there a custom way (preferably without implementing my own method) to verify that the string is being processed using Double.parseDouble() ?

+65
java string floating-point parsing
Aug 22 2018-10-22T00:
source share
6 answers

A general approach would be to validate it with a regular expression, which was also suggested in the documentation of Double.valueOf(String) .

The regex provided there (or the one below) should cover all valid floating point cases, so you don't have to bother with it, as you end up missing some of the thin points.

If you do not want to do this, try catch is still an option.

The regex suggested by JavaDoc is as follows:

 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 } 
+45
Aug 22 '10 at 22:37
source share

Apache , as usual, has a good response from Apache Commons-Lang in the form of NumberUtils.isCreatable(String) .

Handles null s; try / catch not required.

+54
Aug 22 '10 at 23:53
source share

You can always wrap Double.parseDouble () in a catch try block.

 try { Double.parseDouble(number); } catch(NumberFormatException e) { //not a double } 
+51
Aug 22 2018-10-22T00:
source share

Something like below should be enough: -

 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 
+9
Aug 22 2018-10-22T00:
source share

The Google Guava library provides a good helper method for this: Doubles.tryParse(String) . You use it as Double.parseDouble , but it returns null , and does not throw an exception if the string is not parsed for double.

+8
Apr 08 '14 at 12:23
source share

All answers are in order, depending on how academic you want to be. If you want to follow Java specifications exactly, use the following:

 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(); } 

This code is based on JavaDocs on Double .

+7
Apr 18 '13 at 9:05
source share



All Articles