Class BigHelper

java.lang.Object
com.helger.base.numeric.BigHelper

@Immutable public final class BigHelper extends Object
BigDecimal and BigInteger helper
Since:
v12.0.0
Author:
Philip Helger
  • Method Details

    • getDividedBigDecimal

      public static @NonNull BigDecimal getDividedBigDecimal(long nDividend, long nDivisor)
      Get the division result using BigDecimal.
      Parameters:
      nDividend - the dividend
      nDivisor - the divisor
      Returns:
      the result of the division
      Throws:
      ArithmeticException - if the divisor is 0.
    • getDividedBigDecimal

      public static @NonNull BigDecimal getDividedBigDecimal(long nDividend, long nDivisor, @Nonnegative int nScale, @NonNull RoundingMode eRoundingMode)
      Get the division result using BigDecimal.
      Parameters:
      nDividend - the dividend
      nDivisor - the divisor
      nScale - Number of fraction digits. Must be ≥ 0.
      eRoundingMode - Round mode to be used. May not be null.
      Returns:
      the result of the division
      Throws:
      ArithmeticException - if the divisor is 0.
      Since:
      v11.0.2
    • getMaxBigDecimal

      public static @NonNull BigDecimal getMaxBigDecimal(@NonNull BigDecimal aValue, @NonNull BigDecimal... aValues)
      Get the maximum of the passed BigDecimal values.
      Parameters:
      aValue - The first value. May not be null.
      aValues - The remaining values. May not be null.
      Returns:
      The maximum of all passed values. Never null.
    • getMaxBigInteger

      public static @NonNull BigInteger getMaxBigInteger(@NonNull BigInteger aValue, @NonNull BigInteger... aValues)
      Get the maximum of the passed BigInteger values.
      Parameters:
      aValue - The first value. May not be null.
      aValues - The remaining values. May not be null.
      Returns:
      The maximum of all passed values. Never null.
    • getMinBigDecimal

      public static @NonNull BigDecimal getMinBigDecimal(@NonNull BigDecimal aValue, @NonNull BigDecimal... aValues)
      Get the minimum of the passed BigDecimal values.
      Parameters:
      aValue - The first value. May not be null.
      aValues - The remaining values. May not be null.
      Returns:
      The minimum of all passed values. Never null.
    • getMinBigInteger

      public static @NonNull BigInteger getMinBigInteger(@NonNull BigInteger aValue, @NonNull BigInteger... aValues)
      Get the minimum of the passed BigInteger values.
      Parameters:
      aValue - The first value. May not be null.
      aValues - The remaining values. May not be null.
      Returns:
      The minimum of all passed values. Never null.
    • abs

      public static @NonNull BigDecimal abs(@NonNull BigDecimal aValue)
      This is a sanity method wrapping BigDecimal.abs (double), so that you don't have to think whether you need to invoke the abs method from this class or the one from BigDecimal directly.
      Parameters:
      aValue - Input value
      Returns:
      the absolute value of the argument.
    • abs

      public static @NonNull BigInteger abs(@NonNull BigInteger aValue)
      This is a sanity method wrapping BigInteger.abs (double), so that you don't have to think whether you need to invoke the abs method from this class or the one from BigInteger directly.
      Parameters:
      aValue - Input value
      Returns:
      the absolute value of the argument.
    • equalValues

      public static boolean equalValues(@Nullable BigDecimal aObj1, @Nullable BigDecimal aObj2)
      Special equals implementation for BigDecimal because BigDecimal.equals returns false if they have a different scale so that "5.5" is not equal "5.50".
      Parameters:
      aObj1 - first value. May be null.
      aObj2 - second value. May be null.
      Returns:
      true if they contain the same value
    • isEQ0

      public static boolean isEQ0(@NonNull BigDecimal aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is = 0.
    • isNE0

      public static boolean isNE0(@NonNull BigDecimal aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is != 0.
    • isLT0

      public static boolean isLT0(@NonNull BigDecimal aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is < 0.
    • isLE0

      public static boolean isLE0(@NonNull BigDecimal aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is ≤ 0.
    • isGT0

      public static boolean isGT0(@NonNull BigDecimal aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is > 0.
    • isGE0

      public static boolean isGE0(@NonNull BigDecimal aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is ≥ 0.
    • isEQ1

      public static boolean isEQ1(@NonNull BigDecimal aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is = 1.
    • isNE1

      public static boolean isNE1(@NonNull BigDecimal aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is != 1.
    • isLT1

      public static boolean isLT1(@NonNull BigDecimal aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is < 1.
    • isLE1

      public static boolean isLE1(@NonNull BigDecimal aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is ≤ 1.
    • isGT1

      public static boolean isGT1(@NonNull BigDecimal aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is > 1.
    • isGE1

      public static boolean isGE1(@NonNull BigDecimal aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is ≥ 1.
    • isEQ10

      public static boolean isEQ10(@NonNull BigDecimal aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is = 10.
    • isNE10

      public static boolean isNE10(@NonNull BigDecimal aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is != 10.
    • isLT10

      public static boolean isLT10(@NonNull BigDecimal aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is < 10.
    • isLE10

      public static boolean isLE10(@NonNull BigDecimal aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is ≤ 10.
    • isGT10

      public static boolean isGT10(@NonNull BigDecimal aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is > 10.
    • isGE10

      public static boolean isGE10(@NonNull BigDecimal aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is ≥ 10.
    • isEQ100

      public static boolean isEQ100(@NonNull BigDecimal aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is = 100.
    • isNE100

      public static boolean isNE100(@NonNull BigDecimal aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is != 100.
    • isLT100

      public static boolean isLT100(@NonNull BigDecimal aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is < 100.
    • isLE100

      public static boolean isLE100(@NonNull BigDecimal aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is ≤ 100.
    • isGT100

      public static boolean isGT100(@NonNull BigDecimal aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is > 100.
    • isGE100

      public static boolean isGE100(@NonNull BigDecimal aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is ≥ 100.
    • isEQ0

      public static boolean isEQ0(@NonNull BigInteger aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is = 0.
    • isNE0

      public static boolean isNE0(@NonNull BigInteger aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is != 0.
    • isLT0

      public static boolean isLT0(@NonNull BigInteger aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is < 0.
    • isLE0

      public static boolean isLE0(@NonNull BigInteger aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is ≤ 0.
    • isGT0

      public static boolean isGT0(@NonNull BigInteger aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is > 0.
    • isGE0

      public static boolean isGE0(@NonNull BigInteger aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is ≥ 0.
    • isEQ1

      public static boolean isEQ1(@NonNull BigInteger aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is = 1.
    • isNE1

      public static boolean isNE1(@NonNull BigInteger aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is != 1.
    • isLT1

      public static boolean isLT1(@NonNull BigInteger aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is < 1.
    • isLE1

      public static boolean isLE1(@NonNull BigInteger aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is ≤ 1.
    • isGT1

      public static boolean isGT1(@NonNull BigInteger aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is > 1.
    • isGE1

      public static boolean isGE1(@NonNull BigInteger aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is ≥ 1.
    • isEQ10

      public static boolean isEQ10(@NonNull BigInteger aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is = 10.
    • isNE10

      public static boolean isNE10(@NonNull BigInteger aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is != 10.
    • isLT10

      public static boolean isLT10(@NonNull BigInteger aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is < 10.
    • isLE10

      public static boolean isLE10(@NonNull BigInteger aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is ≤ 10.
    • isGT10

      public static boolean isGT10(@NonNull BigInteger aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is > 10.
    • isGE10

      public static boolean isGE10(@NonNull BigInteger aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is ≥ 10.
    • isEQ100

      public static boolean isEQ100(@NonNull BigInteger aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is = 100.
    • isNE100

      public static boolean isNE100(@NonNull BigInteger aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is != 100.
    • isLT100

      public static boolean isLT100(@NonNull BigInteger aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is < 100.
    • isLE100

      public static boolean isLE100(@NonNull BigInteger aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is ≤ 100.
    • isGT100

      public static boolean isGT100(@NonNull BigInteger aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is > 100.
    • isGE100

      public static boolean isGE100(@NonNull BigInteger aValue)
      Parameters:
      aValue - Value to compare. May not be null.
      Returns:
      true if the value is ≥ 100.
    • getWithoutTrailingZeroes

      @CheckReturnValue public static @Nullable BigDecimal getWithoutTrailingZeroes(@Nullable String sValue)
      Get the passed String as a BigDecimal without any trailing zeroes.
      Parameters:
      sValue - The String to be used as a BigDecimal to be modified. May be null.
      Returns:
      null if the input value is null.
    • getWithoutTrailingZeroes

      @CheckReturnValue public static @Nullable BigDecimal getWithoutTrailingZeroes(@Nullable BigDecimal aValue)
      Get the passed BigDecimal without any trailing zeroes. Examples:
      • new BigDecimal ("0.00000000") --> 0
      • new BigDecimal ("10") --> 10
      • new BigDecimal ("10.00000000") --> 10
      • new BigDecimal ("10.1") --> 10.1
      • new BigDecimal ("10.10000000") --> 10.1
      • new BigDecimal ("10.345") --> 10.345
      • new BigDecimal ("10.3450000000") --> 10.345
      Parameters:
      aValue - The BigDecimal to be modified. May be null.
      Returns:
      null if the input value is null.
    • getFractionDigits

      @Nonnegative public static int getFractionDigits(@NonNull BigDecimal aBD)
      Get the number of effective fraction digits by the specified BigDecimal. Examples:
      • new BigDecimal ("10") --> 0
      • new BigDecimal ("10.00000000") --> 0
      • new BigDecimal ("10.1") --> 1
      • new BigDecimal ("10.10000000") --> 1
      • new BigDecimal ("10.345") --> 3
      • new BigDecimal ("10.3450000000") --> 3
      Parameters:
      aBD - The BigDecimal to check. May not be null.
      Returns:
      The minimum number of fraction digits. Always ≥ 0.
    • addPercent

      public static @NonNull BigDecimal addPercent(@NonNull BigDecimal aBase, @NonNull BigDecimal aPercentage)
      Add x% to base
      Parameters:
      aBase - Base value. May not be null.
      aPercentage - Percentage value (0-100). May not be null.
      Returns:
      base + x% (=aBase * (100 + perc) / 100). Never null.
    • addPercent

      public static @NonNull BigDecimal addPercent(@NonNull BigDecimal aBase, @NonNull BigDecimal aPercentage, @Nonnegative int nScale, @NonNull RoundingMode eRoundingMode)
      Add x% to base
      Parameters:
      aBase - Base value. May not be null.
      aPercentage - Percentage value (0-100). May not be null.
      nScale - Maximum scale to achieve.
      eRoundingMode - Rounding mode to used. May not be null.
      Returns:
      base + x% (=aBase * (100 + perc) / 100). Never null.
    • subtractPercent

      public static @NonNull BigDecimal subtractPercent(@NonNull BigDecimal aBase, @NonNull BigDecimal aPercentage)
      Subtract x% from base
      Parameters:
      aBase - Base value. May not be null.
      aPercentage - Percentage value (0-100). May not be null.
      Returns:
      base - x% (=aBase * (100 - perc) / 100). Never null.
    • subtractPercent

      public static @NonNull BigDecimal subtractPercent(@NonNull BigDecimal aBase, @NonNull BigDecimal aPercentage, @Nonnegative int nScale, @NonNull RoundingMode eRoundingMode)
      Subtract x% from base
      Parameters:
      aBase - Base value. May not be null.
      aPercentage - Percentage value (0-100). May not be null.
      nScale - Maximum scale to achieve.
      eRoundingMode - Rounding mode to used. May not be null.
      Returns:
      base - x% (=aBase * (100 - perc) / 100). Never null.
    • getPercentValue

      public static @NonNull BigDecimal getPercentValue(@NonNull BigDecimal aBase, @NonNull BigDecimal aPercentage)
      Get x% from base
      Parameters:
      aBase - Base value. May not be null.
      aPercentage - Percentage value (0-100). May not be null.
      Returns:
      x% from base (=aBase * perc / 100). Never null.
    • getPercentValue

      public static @NonNull BigDecimal getPercentValue(@NonNull BigDecimal aBase, @NonNull BigDecimal aPercentage, @Nonnegative int nScale, @NonNull RoundingMode eRoundingMode)
      Get x% from base with rounding etc.
      Parameters:
      aBase - Base value. May not be null.
      aPercentage - Percentage value (0-100). May not be null.
      nScale - Maximum scale to achieve.
      eRoundingMode - Rounding mode to used. May not be null.
      Returns:
      x% from base (=aBase * perc / 100). Never null.
    • toBigDecimal

      public static @NonNull BigDecimal toBigDecimal(int n)
      Convert an int to a BigDecimal.
      Parameters:
      n - The int value.
      Returns:
      The corresponding BigDecimal. Never null.
    • toBigDecimal

      public static @NonNull BigDecimal toBigDecimal(long n)
      Convert a long to a BigDecimal.
      Parameters:
      n - The long value.
      Returns:
      The corresponding BigDecimal. Never null.
    • toBigDecimal

      public static @NonNull BigDecimal toBigDecimal(float f)
      Convert a float to a BigDecimal.
      Parameters:
      f - The float value.
      Returns:
      The corresponding BigDecimal. Never null.
    • toBigDecimal

      public static @NonNull BigDecimal toBigDecimal(double d)
      Convert a double to a BigDecimal.
      Parameters:
      d - The double value.
      Returns:
      The corresponding BigDecimal. Never null.
    • toBigDecimal

      public static @NonNull BigDecimal toBigDecimal(@NonNull Number aNumber)
      Convert a Number to a BigDecimal.
      Parameters:
      aNumber - The number to convert. May not be null.
      Returns:
      The corresponding BigDecimal. Never null.
    • toBigDecimal

      public static @NonNull BigDecimal toBigDecimal(@NonNull String sNumber)
      Convert a String to a BigDecimal.
      Parameters:
      sNumber - The string representation of the number. May not be null.
      Returns:
      The corresponding BigDecimal. Never null.
    • toBigInteger

      public static @NonNull BigInteger toBigInteger(int n)
      Convert an int to a BigInteger.
      Parameters:
      n - The int value.
      Returns:
      The corresponding BigInteger. Never null.
    • toBigInteger

      public static @NonNull BigInteger toBigInteger(long n)
      Convert a long to a BigInteger.
      Parameters:
      n - The long value.
      Returns:
      The corresponding BigInteger. Never null.
    • toBigInteger

      public static @NonNull BigInteger toBigInteger(@NonNull Number aNumber)
      Convert a Number to a BigInteger.
      Parameters:
      aNumber - The number to convert. May not be null.
      Returns:
      The corresponding BigInteger. Never null.
    • toBigInteger

      public static @NonNull BigInteger toBigInteger(@NonNull String sNumber)
      Convert a String to a BigInteger.
      Parameters:
      sNumber - The string representation of the number. May not be null.
      Returns:
      The corresponding BigInteger. Never null.