Class JavaDoubleParser


  • public final class JavaDoubleParser
    extends java.lang.Object
    Parses a double value; the supported syntax is compatible with Double.valueOf(String).

    Syntax

    Leading and trailing whitespace characters in the string 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 string should constitute a Java FloatingPointLiteral as described by the lexical syntax rules shown below:

    FloatingPointLiteral:
    [Sign] NaN
    [Sign] Infinity
    [Sign] DecimalFloatingPointLiteral
    [Sign] HexFloatingPointLiteral
    HexFloatingPointLiteral:
    HexSignificand BinaryExponent [FloatTypeSuffix]
    HexSignificand:
    HexNumeral
    HexNumeral .
    0x [HexDigits] . HexDigits
    0X [HexDigits] . HexDigits
    BinaryExponent:
    BinaryExponentIndicator SignedInteger
    BinaryExponentIndicator:
    p
    P
    DecimalFloatingPointLiteral:
    DecSignificand [DecExponent] [FloatTypeSuffix]
    DecSignificand:
    IntegerPart . [FractionPart]
    . FractionPart
    IntegerPart
    IntegerPart:
    Digits
    FractionPart:
    Digits
    DecExponent:
    ExponentIndicator [Sign] Digits
    ExponentIndicator:
    e
    E
    Sign:
    ++
    -
    Digits:
    Digit {Digit}
    Digit:
    (one of)
    0 1 2 3 4 5 6 7 8 9
    HexNumeral:
    0 x HexDigits
    0 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 digits
    • IntegerPart: 1 to 7 digits
    • FractionPart: 1 to 16 digits
    • SignedInteger in exponent: 1 to 3 digits
    • FloatingPointLiteral: 1 to 24 characters, e.g. "-1.2345678901234568E-300"
    Maximal input length supported by this parser:
    • 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
    • Constructor Detail

      • JavaDoubleParser

        private JavaDoubleParser()
        Don't let anyone instantiate this class.
    • Method Detail

      • parseDouble

        public static double parseDouble​(java.lang.CharSequence str)
                                  throws java.lang.NumberFormatException
        Convenience method for calling parseDouble(CharSequence, int, int).
        Parameters:
        str - the string to be parsed
        Returns:
        the parsed value
        Throws:
        java.lang.NullPointerException - if the string is null
        java.lang.NumberFormatException - if the string can not be parsed successfully
      • parseDouble

        public static double parseDouble​(java.lang.CharSequence str,
                                         int offset,
                                         int length)
                                  throws java.lang.NumberFormatException
        Parses a FloatingPointLiteral from a CharSequence and converts it into a double value.
        Parameters:
        str - the string to be parsed
        offset - the start offset of the FloatingPointLiteral in str
        length - the length of FloatingPointLiteral in str
        Returns:
        the parsed value
        Throws:
        java.lang.NullPointerException - if the string is null
        java.lang.IllegalArgumentException - if offset or length are illegal
        java.lang.NumberFormatException - if the string can not be parsed successfully
      • parseDouble

        public static double parseDouble​(byte[] str)
                                  throws java.lang.NumberFormatException
        Convenience method for calling parseDouble(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:
        java.lang.NullPointerException - if the string is null
        java.lang.IllegalArgumentException - if offset or length are illegal
        java.lang.NumberFormatException - if the string can not be parsed successfully
      • parseDouble

        public static double parseDouble​(byte[] str,
                                         int offset,
                                         int length)
                                  throws java.lang.NumberFormatException
        Parses a FloatingPointLiteral from a byte-Array and converts it into a double value.
        Parameters:
        str - the string to be parsed, a byte array with characters in ISO-8859-1, ASCII or UTF-8 encoding
        offset - The index of the first byte to parse
        length - The number of bytes to parse
        Returns:
        the parsed value
        Throws:
        java.lang.NullPointerException - if the string is null
        java.lang.IllegalArgumentException - if offset or length are illegal
        java.lang.NumberFormatException - if the string can not be parsed successfully
      • parseDouble

        public static double parseDouble​(char[] str)
                                  throws java.lang.NumberFormatException
        Convenience method for calling parseDouble(char[], int, int).
        Parameters:
        str - the string to be parsed
        Returns:
        the parsed value
        Throws:
        java.lang.NullPointerException - if the string is null
        java.lang.NumberFormatException - if the string can not be parsed successfully
      • parseDouble

        public static double parseDouble​(char[] str,
                                         int offset,
                                         int length)
                                  throws java.lang.NumberFormatException
        Parses a FloatingPointLiteral from a byte-Array and converts it into a double value.

        See JavaDoubleParser for the syntax of FloatingPointLiteral.

        Parameters:
        str - the string to be parsed, a byte array with characters in ISO-8859-1, ASCII or UTF-8 encoding
        offset - The index of the first character to parse
        length - The number of characters to parse
        Returns:
        the parsed value
        Throws:
        java.lang.NullPointerException - if the string is null
        java.lang.IllegalArgumentException - if offset or length are illegal
        java.lang.NumberFormatException - if the string can not be parsed successfully