java.io.Serializable, java.lang.Comparable<Fraction>public class Fraction extends java.lang.Number implements java.lang.Comparable<Fraction>
ints.
The value of the fraction is always kept reduced to the lowest possible terms.
| Modifier and Type | Field | Description |
|---|---|---|
static Fraction |
ONE |
Fraction of
1/1. |
static Fraction |
ONE_HALF |
Fraction of
1/2. |
static Fraction |
ONE_QUARTER |
Fraction of
1/4. |
static Fraction |
ONE_THIRD |
Fraction of
1/3. |
static Fraction |
THREE_QUARTERS |
Fraction of
3/4. |
static Fraction |
TWO_THIRDS |
Fraction of
2/3. |
static Fraction |
ZERO |
Fraction of
0/1. |
| Modifier and Type | Method | Description |
|---|---|---|
Fraction |
abs() |
Returns the absolute value of this fraction.
|
Fraction |
add(int value) |
Returns the sum of this fraction and the given number.
|
Fraction |
add(Fraction other) |
Returns the sum of this fraction and the given fraction.
|
int |
compareTo(Fraction other) |
|
Fraction |
divide(int value) |
Returns the quotient of this fraction and the given number.
|
Fraction |
divide(Fraction other) |
Returns the quotient of this fraction and the given fraction.
|
double |
doubleValue() |
|
boolean |
equals(java.lang.Object other) |
|
float |
floatValue() |
|
int |
getDenominator() |
Returns the denominator of this fraction.
|
int |
getNumerator() |
Returns the numerator of this fraction.
|
int |
hashCode() |
|
int |
intValue() |
|
int |
intValueExact() |
Returns the
int value of this fraction. |
boolean |
isInteger() |
Checks whether the value of this fraction is an integer.
|
long |
longValue() |
|
long |
longValueExact() |
Returns the
long value of this fraction. |
Fraction |
multiply(int value) |
Returns the product of this fraction and the given number.
|
Fraction |
multiply(Fraction other) |
Returns the product of this fraction and the given fraction.
|
Fraction |
negate() |
Returns the opposite of this fraction.
|
static Fraction |
parse(java.lang.String text) |
Returns a fraction obtained from the given text string.
|
Fraction |
pow(int exponent) |
Returns this fraction raised to the power of the given exponent.
|
Fraction |
reciprocal() |
Return the reciprocal of this fraction.
|
int |
round(java.math.RoundingMode roundingMode) |
Returns the integer value nearest to the value of this fraction.
|
int |
signum() |
Returns the signum of this value.
|
Fraction |
subtract(int value) |
Returns the difference of this fraction and the given number.
|
Fraction |
subtract(Fraction other) |
Returns the difference of this fraction and the given fraction.
|
java.math.BigDecimal |
toBigDecimal() |
Returns the
BigDecimal value of this fraction. |
java.math.BigDecimal |
toBigDecimal(int scale,
java.math.RoundingMode roundingMode) |
Returns the
BigDecimal value of this fraction. |
java.lang.String |
toString() |
|
static Fraction |
valueOf(int value) |
Returns a fraction with the given value.
|
static Fraction |
valueOf(int numerator,
int denominator) |
Returns a fraction with the given numerator and denominator.
|
public static final Fraction ONE
1/1.public static final Fraction ONE_HALF
1/2.public static final Fraction ONE_QUARTER
1/4.public static final Fraction ONE_THIRD
1/3.public static final Fraction THREE_QUARTERS
3/4.public static final Fraction TWO_THIRDS
2/3.public static final Fraction ZERO
0/1.public Fraction abs()
java.lang.ArithmeticException - if numerator is equal to Integer.MIN_VALUEpublic Fraction add(Fraction other)
java.lang.IllegalArgumentException - if other is nullpublic Fraction add(int value)
public int compareTo(Fraction other)
compareTo in interface java.lang.Comparable<Fraction>public Fraction divide(Fraction other)
java.lang.ArithmeticException - if other is equal to 0public Fraction divide(int value)
java.lang.ArithmeticException - if value is equal to 0public double doubleValue()
doubleValue in class java.lang.Numberpublic boolean equals(java.lang.Object other)
equals in class java.lang.Objectpublic float floatValue()
floatValue in class java.lang.Numberpublic int getDenominator()
public int getNumerator()
public int hashCode()
hashCode in class java.lang.Objectpublic int intValue()
intValue in class java.lang.Numberpublic boolean isInteger()
public int intValueExact()
int value of this fraction.java.lang.IllegalArgumentException - if the value of this fraction does not exactly fit an intpublic long longValue()
longValue in class java.lang.Numberpublic long longValueExact()
long value of this fraction.java.lang.IllegalArgumentException - if the value of this fraction does not exactly fit a longpublic Fraction multiply(Fraction other)
java.lang.IllegalArgumentException - if other is nullpublic Fraction multiply(int value)
public Fraction negate()
java.lang.ArithmeticException - if numerator is equal to Integer.MIN_VALUEpublic static Fraction parse(java.lang.String text)
java.lang.IllegalArgumentException - if text is nulljava.lang.NumberFormatException - if text is not a valid fractionpublic Fraction pow(int exponent)
public Fraction reciprocal()
java.lang.ArithmeticException - if the value of this fraction is equal to 0public int round(java.math.RoundingMode roundingMode)
java.lang.IllegalArgumentException - if roundingMode is nullpublic int signum()
public Fraction subtract(Fraction other)
java.lang.IllegalArgumentException - if other is nullpublic Fraction subtract(int value)
public java.math.BigDecimal toBigDecimal()
BigDecimal value of this fraction.java.lang.IllegalArgumentException - if the value of this fraction does not have a terminating decimal expansionpublic java.math.BigDecimal toBigDecimal(int scale,
java.math.RoundingMode roundingMode)
BigDecimal value of this fraction.java.lang.IllegalArgumentException - if roundingMode is nullpublic java.lang.String toString()
toString in class java.lang.Objectpublic static Fraction valueOf(int value)
public static Fraction valueOf(int numerator, int denominator)
java.lang.IllegalArgumentException - if denominator is 0