- 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:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classBigFraction.DistributionMethod
-
Field Summary
Fields Modifier and Type Field Description static BigFractionONEstatic BigFractionZERO
-
Constructor Summary
Constructors Constructor Description BigFraction(long numerator, long denominator, boolean displayPercentage)BigFraction(String value)BigFraction(BigInteger numerator, BigInteger denominator, boolean displayPercentage)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description BigFractionabs()Gets the absolute value, but is not reduced.BigFractionadd(BigFraction val)Adds two fractions, returning the value in lowest terms.intcompareTo(BigFraction o)static BigDecimal[]distributeValue(BigDecimal total, BigFraction.DistributionMethod distributionMethod, BigFraction... fractions)Evenly distributes the total value of BigDecimal by fractional amounts.BigFractiondivide(BigFraction val)Divides two fractions, returning the value in lowest terms.doubledoubleValue()booleanequals(Object obj)Two fractions are equal when they have both the same numerator, denominator, and displayPercentage.floatfloatValue()BigDecimalgetBigDecimal(int scale)Gets this fraction as a BigDecimal usingRoundingMode.UNNECESSARY.BigDecimalgetBigDecimal(int scale, RoundingMode roundingMode)Gets this fraction as a BigDecimal using the provided rounding mode.BigIntegergetBigInteger()Gets this fraction as a BigInteger usingRoundingMode.UNNECESSARY.BigIntegergetBigInteger(RoundingMode roundingMode)Gets this fraction as a BigInteger using the provided rounding mode.BigIntegergetDenominator()BigIntegergetNumerator()inthashCode()intintValue()booleanisDisplayPercentage()longlongValue()BigFractionmax(BigFraction val)Gets the higher of the two fractions.BigFractionmin(BigFraction val)Gets the lower of the two fractions.BigFractionmultiply(BigFraction val)Multiplies two fractions, returning the value in lowest terms.BigFractionnegate()Negates the value, but is not reduced.BigFractionpow(int exponent)Raises this fraction to the provided exponent, returning the value in lowest terms.BigFractionreduce()Reduces this fraction to its lowest terms.intsignum()Returns the signum function of thisBigFraction.BigFractionsubtract(BigFraction val)Subtracts two fractions, returning the value in lowest terms.StringtoString()static BigFractionvalueOf(long numerator, long denominator, boolean displayPercentage)static BigFractionvalueOf(BigDecimal value, boolean displayPercentage)Gets the big decimal as a fraction, reduced.static BigFractionvalueOf(BigInteger value, boolean displayPercentage)static BigFractionvalueOf(BigInteger numerator, BigInteger denominator, boolean displayPercentage)-
Methods inherited from class java.lang.Number
byteValue, shortValue
-
-
-
-
Field Detail
-
ZERO
public static final BigFraction ZERO
-
ONE
public static final BigFraction ONE
-
-
Constructor Detail
-
BigFraction
public BigFraction(String value) throws NumberFormatException
- 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 Detail
-
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
-
getNumerator
public BigInteger getNumerator()
-
getDenominator
public BigInteger getDenominator()
-
isDisplayPercentage
public boolean isDisplayPercentage()
-
floatValue
public float floatValue()
- Specified by:
floatValuein classNumber
-
doubleValue
public double doubleValue()
- Specified by:
doubleValuein classNumber
-
getBigInteger
public BigInteger getBigInteger() throws ArithmeticException
Gets this fraction as a BigInteger usingRoundingMode.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 usingRoundingMode.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
-
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, usecompareTo.- Overrides:
equalsin classObject- See Also:
compareTo(BigFraction)
-
compareTo
public int compareTo(BigFraction o)
- Specified by:
compareToin interfaceComparable<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, returnsthis.
-
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, returnsthis.
-
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 thisBigFraction.- Returns:
- -1, 0, or 1 as the value of this
BigFractionis negative, zero, or positive.
-
-