Class NumberUtils

java.lang.Object
software.amazon.smithy.utils.NumberUtils

public final class NumberUtils extends Object
Functions that make working with numbers easier.
  • Method Details

    • parseNumber

      public static Number parseNumber(String lexeme)
      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, trying Long first and falling back to BigInteger for values that overflow long.

      Parameters:
      lexeme - A numeric string.
      Returns:
      A Long, BigInteger, Double, or BigDecimal representing the value.
    • parseDecimalNumber

      public static Number parseDecimalNumber(String lexeme)
      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 Double when the value can be represented without precision loss. Returns a BigDecimal when 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

      public static boolean isDoublePrecisionCompatible(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.

      Returns true unconditionally 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.