public class Rational extends Number implements Comparable<Rational>, Serializable
toString()
in form numerator/denominator
.
Note that any value with a numerator of zero will be treated as zero, even if the
denominator is also zero.Constructor and Description |
---|
Rational(long numerator,
long denominator)
Creates a new instance of Rational.
|
Modifier and Type | Method and Description |
---|---|
byte |
byteValue()
Returns the value of the specified number as a
byte . |
int |
compareTo(Rational that)
Compares two
Rational instances, returning true if they are mathematically
equivalent (in consistence with equals(Object) method). |
double |
doubleValue()
Returns the value of the specified number as a
double . |
boolean |
equals(Object obj)
Compares two
Rational instances, returning true if they are mathematically
equivalent. |
boolean |
equals(Rational other)
Indicates whether this instance and
other are numerically equal,
even if their representations differ. |
boolean |
equalsExact(Rational other)
Indicates whether this instance and
other have identical
Numerator and Denominator. |
float |
floatValue()
Returns the value of the specified number as a
float . |
long |
getDenominator()
Returns the denominator.
|
long |
getNumerator()
Returns the numerator.
|
Rational |
getReciprocal()
Returns the reciprocal value of this object as a new Rational.
|
Rational |
getSimplifiedInstance()
Simplifies the representation of this
Rational number. |
int |
hashCode() |
int |
intValue()
Returns the value of the specified number as an
int . |
boolean |
isInteger()
Checks if this
Rational number is an Integer, either positive or negative. |
boolean |
isZero()
Checks if either the numerator or denominator are zero.
|
long |
longValue()
Returns the value of the specified number as a
long . |
short |
shortValue()
Returns the value of the specified number as a
short . |
String |
toSimpleString(boolean allowDecimal)
Returns the simplest representation of this
Rational 's value possible. |
String |
toString()
Returns a string representation of the object of form
numerator/denominator . |
public Rational(long numerator, long denominator)
public double doubleValue()
double
.
This may involve rounding.doubleValue
in class Number
double
.public float floatValue()
float
.
This may involve rounding.floatValue
in class Number
float
.public final byte byteValue()
byte
.
This may involve rounding or truncation. This implementation simply
casts the result of doubleValue()
to byte
.public final int intValue()
int
.
This may involve rounding or truncation. This implementation simply
casts the result of doubleValue()
to int
.public final long longValue()
long
.
This may involve rounding or truncation. This implementation simply
casts the result of doubleValue()
to long
.public final short shortValue()
short
.
This may involve rounding or truncation. This implementation simply
casts the result of doubleValue()
to short
.shortValue
in class Number
short
.public final long getDenominator()
public final long getNumerator()
public Rational getReciprocal()
public boolean isInteger()
Rational
number is an Integer, either positive or negative.public boolean isZero()
public String toString()
numerator/denominator
.public String toSimpleString(boolean allowDecimal)
Rational
's value possible.public int compareTo(Rational that)
Rational
instances, returning true if they are mathematically
equivalent (in consistence with equals(Object)
method).compareTo
in interface Comparable<Rational>
that
- the Rational
to compare this instance to.0
if this Rational
is
equal to the argument Rational
mathematically; a value less
than 0
if this Rational
is less
than the argument Rational
; and a value greater
than 0
if this Rational
is greater than the argument
Rational
.public boolean equals(Rational other)
other
are numerically equal,
even if their representations differ.
For example, 1/2 is equal to 10/20 by this method.
Similarly, 1/0 is equal to 100/0 by this method.
To test equal representations, use EqualsExact.other
- The rational value to compare withpublic boolean equalsExact(Rational other)
other
have identical
Numerator and Denominator.
For example, 1/2 is not equal to 10/20 by this method. Similarly, 1/0 is not equal to 100/0 by this method. To test numerically equivalence, use Equals(Rational).
other
- The rational value to compare withpublic boolean equals(Object obj)
Rational
instances, returning true if they are mathematically
equivalent.public Rational getSimplifiedInstance()
Simplifies the representation of this Rational
number.
For example, 5/10 simplifies to 1/2 because both Numerator and Denominator share a common factor of 5.
Uses the Euclidean Algorithm to find the greatest common divisor.
Copyright © 2010 - 2020 Adobe. All Rights Reserved