Class KiwiBigDecimals

java.lang.Object
org.kiwiproject.base.KiwiBigDecimals

public final class KiwiBigDecimals extends Object
Utilities for working with BigDecimal.

Neither Guava (e.g. DoubleMath) nor Apache Commons (e.g. NumberUtils) contained these exact conversions when this was originally implemented. The Apache Commons NumberUtils does contain conversion methods that accept a default value for null input, or if zero is OK as the default.

  • Method Details

    • toOptionalDouble

      public static OptionalDouble toOptionalDouble(@Nullable BigDecimal value)
      Converts a nullable BigDecimal to an OptionalDouble. Use this if you need a primitive double and want to avoid boxing.
      Parameters:
      value - the BigDecimal or null
      Returns:
      an OptionalDouble
    • toOptionalDoubleObject

      public static Optional<Double> toOptionalDoubleObject(@Nullable BigDecimal value)
      Converts a nullable BigDecimal to an Optional containing a Double. Use this if you need a Double or if you need to perform further map operations on the value. Optional has map, filter, etc. while OptionalDouble does not.
      Parameters:
      value - the BigDecimal or null
      Returns:
      an Optional of Double
    • toDoubleOrNull

      public static Double toDoubleOrNull(@Nullable BigDecimal value)
      Converts a nullable BigDecimal to a Double or returns null if the given value is null.
      Parameters:
      value - the BigDecimal or null
      Returns:
      converted Double or null
    • requireDouble

      public static double requireDouble(@NonNull BigDecimal value)
      Converts the given BigDecimal to a primitive double.
      Parameters:
      value - the non-null BigDecimal
      Returns:
      a primitive double
      Throws:
      IllegalArgumentException - if the given value is null