Package ch.randelshofer.fastdoubleparser
Class JavaDoubleParser
java.lang.Object
ch.randelshofer.fastdoubleparser.JavaDoubleParser
Parses a
double
value; the supported syntax is compatible with
Double.valueOf(String)
.
Syntax
Leading and trailing whitespace characters in str
are ignored.
Whitespace is removed as if by the String.trim()
method;
that is, characters in the range [U+0000,U+0020].
The rest of str
should constitute a Java FloatingPointLiteral
as described by the lexical syntax rules shown below:
- FloatingPointLiteral:
- [Sign]
NaN
- [Sign]
Infinity
- [Sign] DecimalFloatingPointLiteral
- [Sign] HexFloatingPointLiteral
- SignedInteger
- HexFloatingPointLiteral:
- HexSignificand BinaryExponent [FloatTypeSuffix]
- HexSignificand:
- HexNumeral
- HexNumeral
.
0x
[HexDigits].
HexDigits0X
[HexDigits].
HexDigits
- BinaryExponent:
- BinaryExponentIndicator SignedInteger
- BinaryExponentIndicator:
p
P
- DecimalFloatingPointLiteral:
- DecSignificand [DecExponent] [FloatTypeSuffix]
- DecSignificand:
- IntegerPart
.
[FractionPart].
FractionPart- IntegerPart
- IntegerPart:
- Digits
- FractionPart:
- Digits
- DecExponent:
- ExponentIndicator SignedInteger
- ExponentIndicator:
- e
- E
- SignedInteger:
- [Sign] Digits
- Sign:
- +
- -
- Digits:
- Digit {Digit}
- Digit:
- (one of)
0 1 2 3 4 5 6 7 8 9
- HexNumeral:
0
x
HexDigits0
X
HexDigits
- HexDigits:
- HexDigit {HexDigit}
- HexDigit:
- (one of)
0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F
- FloatTypeSuffix:
- (one of)
f F d D
Expected character lengths for values produced by Double.toString(double)
:
DecSignificand
(IntegerPart
+FractionPart
): 1 to 17 digitsIntegerPart
: 1 to 7 digitsFractionPart
: 1 to 16 digitsSignedInteger
in exponent: 1 to 3 digitsFloatingPointLiteral
: 1 to 24 characters, e.g. "-1.2345678901234568E-300"
FloatingPointLiteral
with or without white space around it:Integer.MAX_VALUE
- 4 = 2,147,483,643 characters.
References:
- The Java® Language Specification, Java SE 18 Edition, Chapter 3. Lexical Structure, 3.10.2. Floating-Point Literals
- docs.oracle.com
-
Method Summary
Modifier and TypeMethodDescriptionstatic double
parseDouble
(byte[] str) Convenience method for callingparseDouble(byte[], int, int)
.static double
parseDouble
(byte[] str, int offset, int length) Parses aFloatingPointLiteral
from abyte
-Array and converts it into adouble
value.static double
parseDouble
(char[] str) Convenience method for callingparseDouble(char[], int, int)
.static double
parseDouble
(char[] str, int offset, int length) Parses aFloatingPointLiteral
from abyte
-Array and converts it into adouble
value.static double
parseDouble
(CharSequence str) Convenience method for callingparseDouble(CharSequence, int, int)
.static double
parseDouble
(CharSequence str, int offset, int length)
-
Method Details
-
parseDouble
Convenience method for callingparseDouble(CharSequence, int, int)
.- Parameters:
str
- the string to be parsed- Returns:
- the parsed value
- Throws:
NullPointerException
- if the string is nullNumberFormatException
- if the string can not be parsed successfully
-
parseDouble
public static double parseDouble(CharSequence str, int offset, int length) throws NumberFormatException - Parameters:
str
- the string to be parsedoffset
- the start offset of theFloatingPointLiteral
instr
length
- the length ofFloatingPointLiteral
instr
- Returns:
- the parsed value
- Throws:
NullPointerException
- if the string is nullIllegalArgumentException
- if offset or length are illegalNumberFormatException
- if the string can not be parsed successfully
-
parseDouble
Convenience method for callingparseDouble(byte[], int, int)
.- Parameters:
str
- the string to be parsed, a byte array with characters in ISO-8859-1, ASCII or UTF-8 encoding- Returns:
- the parsed value
- Throws:
NullPointerException
- if the string is nullIllegalArgumentException
- if offset or length are illegalNumberFormatException
- if the string can not be parsed successfully
-
parseDouble
Parses aFloatingPointLiteral
from abyte
-Array and converts it into adouble
value.- Parameters:
str
- the string to be parsed, a byte array with characters in ISO-8859-1, ASCII or UTF-8 encodingoffset
- The index of the first byte to parselength
- The number of bytes to parse- Returns:
- the parsed value
- Throws:
NullPointerException
- if the string is nullIllegalArgumentException
- if offset or length are illegalNumberFormatException
- if the string can not be parsed successfully
-
parseDouble
Convenience method for callingparseDouble(char[], int, int)
.- Parameters:
str
- the string to be parsed- Returns:
- the parsed value
- Throws:
NullPointerException
- if the string is nullNumberFormatException
- if the string can not be parsed successfully
-
parseDouble
Parses aFloatingPointLiteral
from abyte
-Array and converts it into adouble
value.See
JavaDoubleParser
for the syntax ofFloatingPointLiteral
.- Parameters:
str
- the string to be parsed, a byte array with characters in ISO-8859-1, ASCII or UTF-8 encodingoffset
- The index of the first character to parselength
- The number of characters to parse- Returns:
- the parsed value
- Throws:
NullPointerException
- if the string is nullIllegalArgumentException
- if offset or length are illegalNumberFormatException
- if the string can not be parsed successfully
-