Class NumberUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanisDoublePrecisionCompatible(BigDecimal bigDecimalValue, Double doubleValue) Checks whether the bigDecimalValue is represented in the doubleValue without a loss of precision due to IEEE-754 floating-point representation.static NumberparseDecimalNumber(String lexeme) Parses a decimal (floating-point or scientific notation) string into the most precise Number type that preserves the value.static NumberparseNumber(String lexeme) Parses a numeric string into the most appropriate Number type.
-
Method Details
-
parseNumber
Parses a numeric string into the most appropriate Number type.Extreme values ("Infinity", "NaN", and their signed variants) are recognized first and returned as
Double.Strings containing ".", "e", or "E" are treated as decimal numbers and parsed via
parseDecimalNumber(java.lang.String). All other strings are parsed as integers, tryingLongfirst and falling back toBigIntegerfor values that overflow long.- Parameters:
lexeme- A numeric string.- Returns:
- A Long, BigInteger, Double, or BigDecimal representing the value.
-
parseDecimalNumber
Parses a decimal (floating-point or scientific notation) string into the most precise Number type that preserves the value.Extreme values ("Infinity", "NaN", and their signed variants) are recognized first and returned as
Double.Returns a
Doublewhen the value can be represented without precision loss. Returns aBigDecimalwhen the value exceeds double range (would become infinity) or loses precision in IEEE-754.- Parameters:
lexeme- A numeric string containing ".", "e"/"E", or an extreme value literal.- Returns:
- A Double or BigDecimal representing the parsed value.
-
isDoublePrecisionCompatible
Checks whether the bigDecimalValue is represented in the doubleValue without a loss of precision due to IEEE-754 floating-point representation.Returns
trueunconditionally for infinite and NaN doubles, since those values have no BigDecimal equivalent to compare against.- Parameters:
bigDecimalValue- The value as a BigDecimal.doubleValue- The value as a double.- Returns:
- true if the bigDecimalValue is precisely represented by the doubleValue, or if the doubleValue is infinite or NaN.
-