java.lang.Object
java.lang.Number
com.aoapps.lang.math.BigFraction
- All Implemented Interfaces:
Serializable
,Comparable<BigFraction>
Stores arbitrary size fractions by their numerator and denominator.
- Author:
- AO Industries, Inc.
- See Also:
-
Nested Class Summary
-
Field Summary
-
Constructor Summary
ConstructorDescriptionBigFraction
(long numerator, long denominator, boolean displayPercentage) BigFraction
(String value) BigFraction
(BigInteger numerator, BigInteger denominator, boolean displayPercentage) -
Method Summary
Modifier and TypeMethodDescriptionabs()
Gets the absolute value, but is not reduced.add
(BigFraction val) Adds two fractions, returning the value in lowest terms.int
static BigDecimal[]
distributeValue
(BigDecimal total, BigFraction.DistributionMethod distributionMethod, BigFraction... fractions) Evenly distributes the total value of BigDecimal by fractional amounts.divide
(BigFraction val) Divides two fractions, returning the value in lowest terms.double
boolean
Two fractions are equal when they have both the same numerator, denominator, and displayPercentage.float
getBigDecimal
(int scale) Gets this fraction as a BigDecimal usingRoundingMode.UNNECESSARY
.getBigDecimal
(int scale, RoundingMode roundingMode) Gets this fraction as a BigDecimal using the provided rounding mode.Gets this fraction as a BigInteger usingRoundingMode.UNNECESSARY
.getBigInteger
(RoundingMode roundingMode) Gets this fraction as a BigInteger using the provided rounding mode.int
hashCode()
int
intValue()
boolean
long
max
(BigFraction val) Gets the higher of the two fractions.min
(BigFraction val) Gets the lower of the two fractions.multiply
(BigFraction val) Multiplies two fractions, returning the value in lowest terms.negate()
Negates the value, but is not reduced.pow
(int exponent) Raises this fraction to the provided exponent, returning the value in lowest terms.reduce()
Reduces this fraction to its lowest terms.int
signum()
Returns the signum function of thisBigFraction
.subtract
(BigFraction val) Subtracts two fractions, returning the value in lowest terms.toString()
static BigFraction
valueOf
(long numerator, long denominator, boolean displayPercentage) static BigFraction
valueOf
(BigDecimal value, boolean displayPercentage) Gets the big decimal as a fraction, reduced.static BigFraction
valueOf
(BigInteger value, boolean displayPercentage) static BigFraction
valueOf
(BigInteger numerator, BigInteger denominator, boolean displayPercentage) Methods inherited from class java.lang.Number
byteValue, shortValue
-
Field Details
-
ZERO
-
ONE
-
-
Constructor Details
-
BigFraction
- Throws:
NumberFormatException
-
BigFraction
public BigFraction(long numerator, long denominator, boolean displayPercentage) throws NumberFormatException - Throws:
NumberFormatException
-
BigFraction
public BigFraction(BigInteger numerator, BigInteger denominator, boolean displayPercentage) throws NumberFormatException - Throws:
NumberFormatException
-
-
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 resultsfractions
- 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.
- The sum of the results will equal
-
toString
-
getNumerator
-
getDenominator
-
isDisplayPercentage
public boolean isDisplayPercentage() -
intValue
public int intValue() -
longValue
public long longValue() -
floatValue
public float floatValue()- Specified by:
floatValue
in classNumber
-
doubleValue
public double doubleValue()- Specified by:
doubleValue
in classNumber
-
getBigInteger
Gets this fraction as a BigInteger usingRoundingMode.UNNECESSARY
.- Throws:
ArithmeticException
-
getBigInteger
Gets this fraction as a BigInteger using the provided rounding mode.- Throws:
ArithmeticException
-
getBigDecimal
Gets this fraction as a BigDecimal usingRoundingMode.UNNECESSARY
.- Throws:
ArithmeticException
-
getBigDecimal
Gets this fraction as a BigDecimal using the provided rounding mode.- Throws:
ArithmeticException
-
hashCode
public int hashCode() -
equals
Two fractions are equal when they have both the same numerator, denominator, and displayPercentage. For numerical equality independent of denominator, usecompareTo
. -
compareTo
- Specified by:
compareTo
in interfaceComparable<BigFraction>
-
reduce
Reduces this fraction to its lowest terms. -
add
Adds two fractions, returning the value in lowest terms.If either is a percentage, the result will be a percentage.
-
subtract
Subtracts two fractions, returning the value in lowest terms.If either is a percentage, the result will be a percentage.
-
multiply
Multiplies two fractions, returning the value in lowest terms.If both are percentages, the result will be a percentage.
-
divide
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
Negates the value, but is not reduced. -
abs
Gets the absolute value, but is not reduced. -
max
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, returnsthis
. -
min
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, returnsthis
. -
pow
Raises this fraction to the provided exponent, returning the value in lowest terms. -
signum
public int signum()Returns the signum function of thisBigFraction
.- Returns:
- -1, 0, or 1 as the value of this
BigFraction
is negative, zero, or positive.
-