Class MathUtil

java.lang.Object
com.bluebell.greenhouse.platform.util.math.MathUtil

public final class MathUtil extends Object
General arithmetic and number formatting service.
Version:
2.0.2
Author:
Stephen Prizio
  • Method Details

    • getDouble

      public static double getDouble(double d)
      Gets a rounded Double to 2 decimal places.
      Parameters:
      d - Double
      Returns:
      Double
    • getInteger

      public static int getInteger(double d)
      Gets a rounded Integer.
      Parameters:
      d - Double
      Returns:
      Integer
    • getLong

      public static long getLong(double d)
      Gets a rounded Long.
      Parameters:
      d - Double
      Returns:
      Long
    • toDouble

      public static double toDouble(BigDecimal value)
      Converts a BigDecimal into a rounded double.
      Parameters:
      value - value
      Returns:
      rounded double
    • orZero

      public static BigDecimal orZero(BigDecimal value)
      Returns zero when the value is null.
      Parameters:
      value - value
      Returns:
      non-null value
    • max

      public static BigDecimal max(BigDecimal... values)
      Returns the maximum value.
      Parameters:
      values - values
      Returns:
      maximum value
    • toWholeNumber

      public static int toWholeNumber(BigDecimal value)
      Rounds to the nearest whole number.
      Parameters:
      value - value
      Returns:
      rounded number
    • toPercentage

      public static int toPercentage(BigDecimal value)
      Converts a fractional score into a whole-number percentage.
      Parameters:
      value - fractional score
      Returns:
      percentage
    • clamp

      public static int clamp(int value, int min, int max)
      Clamps a number between a minimum and maximum value.
      Parameters:
      value - value
      min - minimum value
      max - maximum value
      Returns:
      clamped value
    • percentagePosition

      public static int percentagePosition(BigDecimal value, BigDecimal min, BigDecimal max)
      Resolves a value's position in a range as a 0 to 100 percentage.
      Parameters:
      value - value
      min - minimum range value
      max - maximum range value
      Returns:
      percentage position
    • add

      public static BigDecimal add(BigDecimal a, BigDecimal b)
      Performs a basic BigDecimal sum.
      Parameters:
      a - first value
      b - second value
      Returns:
      sum
    • subtract

      public static BigDecimal subtract(BigDecimal a, BigDecimal b)
      Performs a basic BigDecimal difference.
      Parameters:
      a - first value
      b - second value
      Returns:
      difference
    • multiply

      public static BigDecimal multiply(BigDecimal a, BigDecimal b)
      Performs a basic BigDecimal multiply.
      Parameters:
      a - first value
      b - second value
      Returns:
      product
    • divide

      public static BigDecimal divide(BigDecimal a, BigDecimal b)
      Performs a safe BigDecimal divide.
      Parameters:
      a - dividend
      b - divisor
      Returns:
      quotient
    • divide

      public static BigDecimal divide(BigDecimal a, BigDecimal b, int scale, RoundingMode roundingMode)
      Performs a safe BigDecimal divide.
      Parameters:
      a - dividend
      b - divisor
      scale - result scale
      roundingMode - rounding mode
      Returns:
      quotient
    • add

      public static double add(double a, double b)
      Performs a basic sum.
      Parameters:
      a - Double
      b - Double
      Returns:
      Double
    • subtract

      public static double subtract(double a, double b)
      Performs a basic difference.
      Parameters:
      a - Double
      b - Double
      Returns:
      Double
    • divide

      public static double divide(double a, double b)
      Performs a basic divide with safe dividend.
      Parameters:
      a - Double
      b - Double
      Returns:
      Double
    • multiply

      public static double multiply(double a, double b)
      Performs a basic multiply.
      Parameters:
      a - Double
      b - Double
      Returns:
      Double
    • delta

      public static double delta(double a, double b)
      Obtains the percentage change rounded to a number larger than 1 Example: 9 / 87 would normally be 0.103448, this method would return 10.34.
      Parameters:
      a - Double
      b - Double
      Returns:
      Double
    • wholePercentage

      public static int wholePercentage(double a, double b)
      Obtains a whole percentage rounded to the nearest integer Example : 3 / 10 = 33.
      Parameters:
      a - Double
      b - Double
      Returns:
      Double
    • computeIncrement

      public static double computeIncrement(double a, double b, boolean absolute)
      Computes the increment change on a by b.
      Parameters:
      a - Double starter value
      b - Double value to increment
      absolute - if true, b is considered fixed, else is treated like a percentage
      Returns:
      a + increment (b)
    • percentageChange

      public static double percentageChange(double a, double b)
      Obtains the percentage change between 2 numbers.
      Parameters:
      a - Double
      b - Double
      Returns:
      Double as a percentage
    • weightedAverage

      public static double weightedAverage(double a, double countA, double b, double countB)
      Computes the average of 2 different averages.
      Parameters:
      a - first average
      countA - count a
      b - second average
      countB - count b
      Returns:
      average of both averages