Class BigFraction

java.lang.Object
java.lang.Number
com.aoapps.lang.math.BigFraction
All Implemented Interfaces:
Serializable, Comparable<BigFraction>

public class BigFraction extends Number implements Serializable, Comparable<BigFraction>
Stores arbitrary size fractions by their numerator and denominator.
Author:
AO Industries, Inc.
See Also:
  • Field Details

  • Constructor Details

  • Method Details

    • valueOf

      public static BigFraction valueOf(long numerator, long denominator, boolean displayPercentage) throws NumberFormatException
      Throws:
      NumberFormatException
    • valueOf

      public static BigFraction valueOf(BigInteger value, boolean displayPercentage) throws NumberFormatException
      Throws:
      NumberFormatException
    • valueOf

      public static BigFraction valueOf(BigDecimal value, boolean displayPercentage) throws NumberFormatException
      Gets the big decimal as a fraction, reduced.
      Throws:
      NumberFormatException
    • valueOf

      public static BigFraction valueOf(BigInteger numerator, BigInteger denominator, boolean displayPercentage) throws NumberFormatException
      Throws:
      NumberFormatException
    • distributeValue

      public static BigDecimal[] distributeValue(BigDecimal total, BigFraction.DistributionMethod distributionMethod, BigFraction... fractions)
      Evenly distributes the total value of BigDecimal by fractional amounts.
      • The sum of the results will equal total.
      • The results will use the same scale as total.
      • The results will be rounded where necessary to match the scale of total.
      • Each result will be zero or have a sign matching total.
      Parameters:
      total - The total value to be distributed within the results
      fractions - The fractional amount of each result, the sum must be equal to one. The array elements are unmodified.
      Returns:
      the results corresponding to each fractional amount.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getNumerator

      public BigInteger getNumerator()
    • getDenominator

      public BigInteger getDenominator()
    • isDisplayPercentage

      public boolean isDisplayPercentage()
    • intValue

      public int intValue()
      Specified by:
      intValue in class Number
    • longValue

      public long longValue()
      Specified by:
      longValue in class Number
    • floatValue

      public float floatValue()
      Specified by:
      floatValue in class Number
    • doubleValue

      public double doubleValue()
      Specified by:
      doubleValue in class Number
    • getBigInteger

      public BigInteger getBigInteger() throws ArithmeticException
      Gets this fraction as a BigInteger using RoundingMode.UNNECESSARY.
      Throws:
      ArithmeticException
    • getBigInteger

      public BigInteger getBigInteger(RoundingMode roundingMode) throws ArithmeticException
      Gets this fraction as a BigInteger using the provided rounding mode.
      Throws:
      ArithmeticException
    • getBigDecimal

      public BigDecimal getBigDecimal(int scale) throws ArithmeticException
      Gets this fraction as a BigDecimal using RoundingMode.UNNECESSARY.
      Throws:
      ArithmeticException
    • getBigDecimal

      public BigDecimal getBigDecimal(int scale, RoundingMode roundingMode) throws ArithmeticException
      Gets this fraction as a BigDecimal using the provided rounding mode.
      Throws:
      ArithmeticException
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Two fractions are equal when they have both the same numerator, denominator, and displayPercentage. For numerical equality independent of denominator, use compareTo.
      Overrides:
      equals in class Object
      See Also:
    • compareTo

      public int compareTo(BigFraction o)
      Specified by:
      compareTo in interface Comparable<BigFraction>
    • reduce

      public BigFraction reduce()
      Reduces this fraction to its lowest terms.
    • add

      public BigFraction add(BigFraction val)
      Adds two fractions, returning the value in lowest terms.

      If either is a percentage, the result will be a percentage.

    • subtract

      public BigFraction subtract(BigFraction val)
      Subtracts two fractions, returning the value in lowest terms.

      If either is a percentage, the result will be a percentage.

    • multiply

      public BigFraction multiply(BigFraction val)
      Multiplies two fractions, returning the value in lowest terms.

      If both are percentages, the result will be a percentage.

    • divide

      public BigFraction divide(BigFraction val)
      Divides two fractions, returning the value in lowest terms.
      • If percent divide by percent: percent
      • If percent divide by non-percent: percent
      • If non-percent divide by percent: non-percent
      • If non-percent divide by non-percent: non-percent
    • negate

      public BigFraction negate()
      Negates the value, but is not reduced.
    • abs

      public BigFraction abs()
      Gets the absolute value, but is not reduced.
    • max

      public BigFraction max(BigFraction val)
      Gets the higher of the two fractions. When they are equal the one with the lower denominator is returned. When their denominators are also equal, returns this.
    • min

      public BigFraction min(BigFraction val)
      Gets the lower of the two fractions. When they are equal the one with the lower denominator is returned. When their denominators are also equal, returns this.
    • pow

      public BigFraction pow(int exponent)
      Raises this fraction to the provided exponent, returning the value in lowest terms.
    • signum

      public int signum()
      Returns the signum function of this BigFraction.
      Returns:
      -1, 0, or 1 as the value of this BigFraction is negative, zero, or positive.