Package ch.randelshofer.fastdoubleparser
Class JavaBigDecimalParser
java.lang.Object
ch.randelshofer.fastdoubleparser.JavaBigDecimalParser
Parses a
BigDecimal
value; the supported syntax is compatible with
BigDecimal(String)
.
Syntax
Parses a BigDecimalString
that is compatible with
the grammar specified in BigDecimal(String)
.
Formal specification of the grammar:
- BigDecimalString:
- [Sign] Significand [Exponent]
- Sign:
- +
- -
- Significand:
- IntegerPart
.
[FractionPart].
FractionPart- IntegerPart
- IntegerPart:
- Digits
- FractionPart:
- Digits
- DecExponent:
- ExponentIndicator SignedInteger
- ExponentIndicator:
- e
- E
- SignedInteger:
- [Sign] Digits
- Digits:
- Digit {Digit}
Character lengths accepted by BigInteger(String)
:
Significand
: 1 to 1,292,782,621 decimal digits.The resulting value must fit into
2^31 - 1
bits. The decimal representation of the value2^31 - 1
has 646,456,993 digits. Therefore an input String can only contain up to that many significant digits - the remaining digits must be leading zeroes.SignedInteger
in exponent: 1 to 10 digits. Exponents with more digits would yield to aBigDecimal.scale()
that does not fit into aint
-value.BigDecimalString
: 1 to 1,292,782,621+4+10=1,292,782,635 characters, e.g. "-1.234567890....12345E-2147483647".
BigDecimalString
: 1,292,782,635 characters.
References:
- Java SE 17 & JDK 17, JavaDoc, Class BigDecimal
- docs.oracle.com
-
Method Summary
Modifier and TypeMethodDescriptionstatic BigDecimal
parseBigDecimal
(byte[] str) Convenience method for callingparseBigDecimal(byte[], int, int)
.static BigDecimal
parseBigDecimal
(byte[] str, int offset, int length) static BigDecimal
parseBigDecimal
(char[] str) Convenience method for callingparseBigDecimal(char[], int, int)
.static BigDecimal
parseBigDecimal
(char[] str, int offset, int length) Parses aFloatingPointLiteral
from abyte
-Array and converts it into adouble
value.static BigDecimal
Convenience method for callingparseBigDecimal(CharSequence, int, int)
.static BigDecimal
parseBigDecimal
(CharSequence str, int offset, int length)
-
Method Details
-
parseBigDecimal
Convenience method for callingparseBigDecimal(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
-
parseBigDecimal
public static BigDecimal parseBigDecimal(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
-
parseBigDecimal
Convenience method for callingparseBigDecimal(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 nullNumberFormatException
- if the string can not be parsed successfully
-
parseBigDecimal
public static BigDecimal parseBigDecimal(byte[] str, int offset, int length) throws NumberFormatException - 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
-
parseBigDecimal
Convenience method for callingparseBigDecimal(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
-
parseBigDecimal
public static BigDecimal parseBigDecimal(char[] str, int offset, int length) throws NumberFormatException Parses aFloatingPointLiteral
from abyte
-Array and converts it into adouble
value.See
JavaBigDecimalParser
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
-