public final class Fraction extends Number implements Comparable<Fraction>
Number implementation that represents fractions accurately. Instances
of this class are immutable.| Modifier and Type | Field and Description |
|---|---|
static Fraction |
ONE
Fraction representation of 1. |
static Fraction |
ONE_HALF
Fraction representation of 1/2. |
static Fraction |
ONE_QUARTER
Fraction representation of 1/4. |
static Fraction |
ONE_THIRD
Fraction representation of 1/3. |
static Fraction |
THREE_QUARTERS
Fraction representation of 3/4. |
static Fraction |
TWO_QUARTERS
Fraction representation of 2/4. |
static Fraction |
TWO_THIRDS
Fraction representation of 2/3. |
static Fraction |
ZERO
Fraction representation of 0. |
| Constructor and Description |
|---|
Fraction(BigInteger numerator,
BigInteger denominator)
Creates a new
Fraction. |
Fraction(int numerator,
int denominator)
Creates a new
Fraction. |
| Modifier and Type | Method and Description |
|---|---|
Fraction |
absoluteValue()
Returns a
Fraction which is the absolute value of this one. |
BigDecimal |
bigDecimalValue()
Returns the value of this
Fraction as a BigDecimal,
rounded in accordance to IEEE 754R Decimal128 format. |
int |
compareTo(Fraction f) |
BigInteger |
denominator()
Returns this
Fraction's denominator. |
Fraction |
dividedBy(Fraction f)
Divides this value by the given one and returns the result in reduced
form.
|
double |
doubleValue() |
boolean |
equals(Object o) |
float |
floatValue() |
int |
hashCode() |
int |
intValue() |
Fraction |
inverted()
Returns the inverse of this
Fraction. |
long |
longValue() |
Fraction |
minus(Fraction f)
Subtracts the given value from this one and returns the result in
reduced form.
|
Fraction |
multipliedBy(Fraction f)
Multiplies this value by the given one and returns the result in
reduced form.
|
Fraction |
negated()
Returns the opposite of this
Fraction. |
BigInteger |
numerator()
Returns this
Fraction's numerator. |
static Fraction |
parse(String str)
Creates a
Fraction from its String representation. |
Fraction |
plus(Fraction f)
Adds the given value to this one and returns the result in reduced
form.
|
Fraction |
power(int exponent)
Returns the
Fraction obtained by raising this to the given
power, in reduced form. |
Fraction |
reduced()
Returns the reduced form of this
Fraction. |
int |
sign()
Returns the sign of this
Fraction, that is, this method
returns 1 if this fraction is positive, 0 if it is
equal to 0, and -1 if it is negative. |
String |
toString() |
static Fraction |
valueOf(BigInteger val)
Returns the
Fraction representation of the given value. |
static Fraction |
valueOf(int val)
Returns the
Fraction representation of the given value. |
static Fraction |
valueOf(Integer val)
Returns the
Fraction representation of the given value. |
byteValue, shortValuepublic static final Fraction ZERO
Fraction representation of 0.public static final Fraction ONE
Fraction representation of 1.public static final Fraction ONE_HALF
Fraction representation of 1/2.public static final Fraction ONE_THIRD
Fraction representation of 1/3.public static final Fraction TWO_THIRDS
Fraction representation of 2/3.public static final Fraction ONE_QUARTER
Fraction representation of 1/4.public static final Fraction TWO_QUARTERS
Fraction representation of 2/4.public static final Fraction THREE_QUARTERS
Fraction representation of 3/4.public Fraction(int numerator,
int denominator)
Fraction.numerator - the numerator.denominator - the denominator.IllegalArgumentException - if denominator equals zero.public Fraction(BigInteger numerator, BigInteger denominator)
Fraction.numerator - the numerator.denominator - the denominator.NullPointerException - if one of the arguments is null.IllegalArgumentException - if denominator equals zero.public static Fraction valueOf(int val)
Fraction representation of the given value.val - an int value.Fraction representation of the given value.public static Fraction valueOf(Integer val)
Fraction representation of the given value.val - an Integer value.Fraction representation of the given value.NullPointerException - if val is null.public static Fraction valueOf(BigInteger val)
Fraction representation of the given value.val - a BigInteger value.Fraction representation of the given value.NullPointerException - if val is null.public static Fraction parse(String str)
Fraction from its String representation.str - the Fraction's String representation.Fraction.IllegalArgumentException - if str doesn't represent a
Fraction or if its denominator equals zero.public Fraction plus(Fraction f)
f - the fraction to add to this one.this + f in reduced form.NullPointerException - if f is null.public Fraction minus(Fraction f)
f - the fraction to subtract from this one.this - f in reduced form.NullPointerException - if f is null.public Fraction multipliedBy(Fraction f)
f - the fraction to multiply this one with.this * f in reduced form.NullPointerException - if f is null.public Fraction dividedBy(Fraction f)
f - the fraction to divide this one with.this / f in reduced form.NullPointerException - if f is null.ArithmeticException - if f equals zero.public Fraction power(int exponent)
Fraction obtained by raising this to the given
power, in reduced form.exponent - the power to raise this fraction to.this ^ exponent in reduced form.ArithmeticException - if exponent is negative.public int sign()
Fraction, that is, this method
returns 1 if this fraction is positive, 0 if it is
equal to 0, and -1 if it is negative.Fraction.public Fraction absoluteValue()
Fraction which is the absolute value of this one.Fraction.public Fraction negated()
Fraction. The result is not
reduced before being returned.-this.public Fraction inverted()
Fraction. The result is not
reduced before being returned.1 / this.public Fraction reduced()
Fraction. Also, note that
the sign information will be held by the numerator.Fraction.public BigInteger numerator()
Fraction's numerator.Fraction's numerator.public BigInteger denominator()
Fraction's denominator.Fraction's denominator.public BigDecimal bigDecimalValue()
Fraction as a BigDecimal,
rounded in accordance to IEEE 754R Decimal128 format.Fraction as a BigDecimal.public float floatValue()
floatValue in class Numberpublic double doubleValue()
doubleValue in class Numberpublic int compareTo(Fraction f)
compareTo in interface Comparable<Fraction>Copyright © 2012–2015. All rights reserved.