public final class Rational extends Number implements Comparable<Rational>
Modifier and Type | Field and Description |
---|---|
static Rational |
HALF
Convenient constant representing a value of a half (1/2 as a rational).
|
static Rational |
ONE
Convenient constant representing a value of one (1/1 as a rational).
|
static Rational |
QUARTER
Convenient constant representing a value of a quarter (1/4 as a rational).
|
static Rational |
THIRD
Convenient constant representing a value of a third (1/3 as a rational).
|
static Rational |
THREE_QUARTERS
Convenient constant representing a value of three quarters (3/4 as a rational).
|
static Rational |
TWO_THIRDS
Convenient constant representing a value of two thirds (2/3 as a rational).
|
static Rational |
ZERO
Convenient constant representing a value of zero (0/1 as a rational).
|
Constructor and Description |
---|
Rational(BigDecimal value)
Creates a rational value equivalent to the specified decimal value.
|
Rational(long value)
Creates a rational value equivalent to the specified integer value.
|
Rational(long numerator,
long denominator)
Creates a vulgar fraction with the specified numerator and denominator.
|
Modifier and Type | Method and Description |
---|---|
Rational |
add(Rational value)
Add the specified value to this value and return the result as a new object
(also a rational).
|
int |
compareTo(Rational other)
Compares this value with the specified object for order.
|
Rational |
divide(Rational value)
Divide this rational by the specified value and return the result as a new
object (also a Rational).
|
double |
doubleValue()
Returns the result of dividing the numerator by the denominator.
|
boolean |
equals(Object other)
Determines whether this rational value is equal to some other object.
|
float |
floatValue()
Returns the result of dividing the numerator by the denominator.
|
long |
getDenominator()
Returns the denominator (divisor) of the fraction.
|
long |
getNumerator()
Returns the numerator of the fraction.
|
int |
hashCode()
Over-ridden to be consistent with
equals(Object) . |
int |
intValue()
Returns the integer equivalent of this rational number.
|
long |
longValue()
Returns the integer equivalent of this rational number as a long.
|
Rational |
multiply(Rational value)
Multiply this rational by the specified value and return the result as a new
object (also a Rational).
|
Rational |
subtract(Rational value)
Subtract the specified value from this value and return the result as a new object
(also a rational).
|
String |
toString()
Returns a String representation of the rational number, expressed as
a vulgar fraction (i.e.
|
byteValue, shortValue
public static final Rational ZERO
public static final Rational QUARTER
public static final Rational THIRD
public static final Rational HALF
public static final Rational TWO_THIRDS
public static final Rational THREE_QUARTERS
public static final Rational ONE
public Rational(long numerator, long denominator)
numerator
- The fraction's numerator (may be negative).denominator
- The fraction's denominator (must be greater than or
equal to 1).public Rational(long value)
value
- The value of this rational as an integer.public Rational(BigDecimal value)
value
- The value of this rational as a fractional decimal.ArithmeticException
- If the BigDecimal value is too large to be
represented as a Rational.public long getNumerator()
public long getDenominator()
public Rational add(Rational value)
value
- The value to add to this rational.public Rational subtract(Rational value)
value
- The value to subtract from this rational.public Rational multiply(Rational value)
value
- The amount to multiply by.public Rational divide(Rational value)
value
- The amount to divide by.public int intValue()
doubleValue()
).public long longValue()
doubleValue()
).public float floatValue()
floatValue
in class Number
public double doubleValue()
doubleValue
in class Number
public boolean equals(Object other)
public int hashCode()
equals(Object)
.public String toString()
public int compareTo(Rational other)
compareTo
in interface Comparable<Rational>
other
- Another Rational value.Copyright © 2015. All rights reserved.