Class BigDecimalUtils

    • Method Detail

      • compare

        public static int compare​(BigDecimal n1,
                                  BigDecimal n2,
                                  int p)
        Compare two BigDecimals up to a precision. In other words, if the absolute difference between the two numbers falls below a threshold, they are considered equal.
        Parameters:
        n1 - a BigDecimal
        n2 - a BigDecimal
        p - the threshold is 1e-p
        Returns:
        -1, 0, or 1 when n1 is numerically less than, equal to, or greater than n2, respectively
      • equals

        public static boolean equals​(BigDecimal n1,
                                     BigDecimal n2,
                                     int precision)
        Check if two BigDecimals are equal up to a precision.
        Parameters:
        n1 - a BigDecimal
        n2 - a BigDecimal
        precision - the threshold is 1e-p
        Returns:
        true if the numbers are equal up to a precision
      • sum

        public static BigDecimal sum​(BigDecimal... big)
        Sum up the BigDecimal numbers.
        Parameters:
        big - BigDecimal numbers
        Returns:
        the sum
      • sum

        public static BigDecimal sum​(double... big)
        Sum up big numbers.
        Parameters:
        big - numbers
        Returns:
        the sum
      • getWhole

        public static BigDecimal getWhole​(BigDecimal num)
        Get the integral part of a number (discarding the fractional part).
        Parameters:
        num - a BigDecimal
        Returns:
        the integral part of the number
      • getFractional

        public static BigDecimal getFractional​(BigDecimal num)
        Get the fractional part of a number. This is the same as the number subtracting the whole part. For a -ve. number, the fractional part is also -ve. For example, for -3.1415, the whole is -3 and the fractional part is -0.1415.
        Parameters:
        num - a BigDecimal
        Returns:
        the fractional part of the number
      • pow

        public static BigDecimal pow​(BigDecimal a,
                                     BigDecimal b,
                                     int scale)
        Compute a to the power of b.
        Parameters:
        a - a base
        b - an exponent
        scale - a precision parameter as in BigDecimal
        Returns:
        ab
      • pow

        public static BigDecimal pow​(BigDecimal a,
                                     int n)
        Compute a to the power of n, where n is an integer.
        Parameters:
        a - a base
        n - an integer exponent
        Returns:
        an
      • pow

        public static BigDecimal pow​(BigDecimal a,
                                     int n,
                                     int scale)
        Compute a to the power of n, where n is an integer. This is simply a wrapper around BigDecimal.pow(int) but handles also negative exponents. Use BigDecimal.pow(int) for arbitrary precision if the exponent is positive.
        Parameters:
        a - a base
        n - an exponent
        scale - a precision parameter as in BigDecimal
        Returns:
        an