@Immutable public final class Fraction extends Number implements Comparable<Fraction>, Immutable
Note: it's copied from Apache Commons Lang developed at The Apache Software Foundation (http://www.apache.org/), or under the Apache License 2.0. The methods copied from other products/frameworks may be modified in this class.
Fraction is a Number implementation that stores fractions accurately.
This class is immutable, and interoperable with most methods that accept a Number.
Note that this class is intended for common use cases, it is int based and thus suffers from various overflow issues. For a BigInteger based equivalent, please see the Commons Math BigFraction class.
| Modifier and Type | Field and Description |
|---|---|
static Fraction |
FOUR_FIFTHS
Fraction representation of 4/5. |
static Fraction |
ONE
Fraction representation of 1. |
static Fraction |
ONE_FIFTH
Fraction representation of 1/5. |
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_FIFTHS
Fraction representation of 3/5. |
static Fraction |
THREE_QUARTERS
Fraction representation of 3/4. |
static Fraction |
TWO_FIFTHS
Fraction representation of 2/5. |
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. |
| Modifier and Type | Method and Description |
|---|---|
Fraction |
abs()
Gets a fraction that is the positive equivalent of this one.
|
Fraction |
add(Fraction fraction)
Adds the value of this fraction to another, returning the result in reduced form.
|
int |
compareTo(Fraction other)
Compares this object to another based on size.
|
int |
denominator() |
Fraction |
dividedBy(Fraction fraction)
Divide the value of this fraction by another.
|
double |
doubleValue()
Gets the fraction as a
double. |
boolean |
equals(Object obj)
Compares this fraction to another object to test if they are equal.
|
float |
floatValue()
Gets the fraction as a
float. |
int |
getDenominator()
Deprecated.
replaced by
denominator |
int |
getNumerator()
Deprecated.
replaced by
numerator |
int |
getProperNumerator()
Deprecated.
replaced by
properNumerator |
int |
getProperWhole()
Deprecated.
replaced by
properWhole |
int |
hashCode()
Gets a hashCode for the fraction.
|
int |
intValue()
Gets the fraction as an
int. |
Fraction |
invert()
Gets a fraction that is the inverse (1/fraction) of this one.
|
long |
longValue()
Gets the fraction as a
long. |
Fraction |
multipliedBy(Fraction fraction)
Multiplies the value of this fraction by another, returning the result in reduced form.
|
Fraction |
negate()
Gets a fraction that is the negative (-fraction) of this one.
|
int |
numerator() |
static Fraction |
of(double value)
Creates a
Fraction instance from a double value. |
static Fraction |
of(int numerator,
int denominator) |
static Fraction |
of(int numerator,
int denominator,
boolean reduce)
Creates a
Fraction instance with the 2 parts of a fraction Y/Z. |
static Fraction |
of(int whole,
int numerator,
int denominator) |
static Fraction |
of(int whole,
int numerator,
int denominator,
boolean reduce)
Creates a
Fraction instance with the 3 parts of a fraction X Y/Z. |
static Fraction |
of(String str)
Creates a Fraction from a
String. |
Fraction |
pow(int power)
Gets a fraction that is raised to the passed in power.
|
int |
properNumerator() |
int |
properWhole() |
Fraction |
reduce()
Reduce the fraction to the smallest values for the numerator and denominator, returning the result.
|
Fraction |
subtract(Fraction fraction)
Subtracts the value of another fraction from the value of this one, returning the result in reduced form.
|
String |
toProperString()
Gets the fraction as a proper
String in the format X Y/Z. |
String |
toString()
Gets the fraction as a
String. |
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 static final Fraction ONE_FIFTH
Fraction representation of 1/5.public static final Fraction TWO_FIFTHS
Fraction representation of 2/5.public static final Fraction THREE_FIFTHS
Fraction representation of 3/5.public static final Fraction FOUR_FIFTHS
Fraction representation of 4/5.public static Fraction of(int numerator, int denominator)
numerator - denominator - of(int, int, boolean)public static Fraction of(int numerator, int denominator, boolean reduce)
Creates a Fraction instance with the 2 parts of a fraction Y/Z.
Any negative signs are resolved to be on the numerator.
numerator - the numerator, for example the three in 'three sevenths'denominator - the denominator, for example the seven in 'three sevenths'reduce - if it's true, reduce Fraction instance with the 2 parts of a fraction Y/Z. For example,
if the input parameters represent 2/4, then the created fraction will be 1/2.ArithmeticException - if the denominator is zero or the denominator is negative and the numerator is
Integer#MIN_VALUEpublic static Fraction of(int whole, int numerator, int denominator)
whole - numerator - denominator - #of(int, int, int, boolean)}public static Fraction of(int whole, int numerator, int denominator, boolean reduce)
Creates a Fraction instance with the 3 parts of a fraction X Y/Z.
The negative sign must be passed in on the whole number part.
whole the whole number, for example the one in 'one and three sevenths' the numerator, for example the three in 'one and three sevenths' the denominator, for example the seven in 'one and three sevenths'reduce - ArithmeticException - if the resulting numerator exceeds Integer.MAX_VALUEpublic static Fraction of(double value)
Creates a Fraction instance from a double value.
This method uses the continued fraction algorithm, computing a maximum of 25 convergents and bounding the denominator by 10,000.
value the double value to convertArithmeticException - if the the algorithm does not convergepublic static Fraction of(String str)
Creates a Fraction from a String.
The formats accepted are:
double String containing a dotand a .
str - the string to parse, must not be nullIllegalArgumentException - if the string is nullNumberFormatException - if the number format is invalid@Deprecated public int getNumerator()
numeratorGets the numerator part of the fraction.
This method may return a value greater than the denominator, an improper fraction, such as the seven in 7/4.
public int numerator()
@Deprecated public int getDenominator()
denominatorGets the denominator part of the fraction.
public int denominator()
@Deprecated public int getProperNumerator()
properNumeratorGets the proper numerator, always positive.
An improper fraction 7/4 can be resolved into a proper one, 1 3/4. This method returns the 3 from the proper fraction.
If the fraction is negative such as -7/4, it can be resolved into -1 3/4, so this method returns the positive proper numerator, 3.
public int properNumerator()
@Deprecated public int getProperWhole()
properWholeGets the proper whole part of the fraction.
An improper fraction 7/4 can be resolved into a proper one, 1 3/4. This method returns the 1 from the proper fraction.
If the fraction is negative such as -7/4, it can be resolved into -1 3/4, so this method returns the positive whole part -1.
public int properWhole()
public int intValue()
Gets the fraction as an int. This returns the whole number part of the fraction.
public long longValue()
Gets the fraction as a long. This returns the whole number part of the fraction.
public float floatValue()
Gets the fraction as a float. This calculates the fraction as the numerator divided by denominator.
floatValue in class Numberpublic double doubleValue()
Gets the fraction as a double. This calculates the fraction as the numerator divided by denominator.
doubleValue in class Numberpublic Fraction reduce()
Reduce the fraction to the smallest values for the numerator and denominator, returning the result.
For example, if this fraction represents 2/4, then the result will be 1/2.
public Fraction invert()
Gets a fraction that is the inverse (1/fraction) of this one.
The returned fraction is not reduced.
ArithmeticException - if the fraction represents zero.public Fraction negate()
Gets a fraction that is the negative (-fraction) of this one.
The returned fraction is not reduced.
public Fraction abs()
Gets a fraction that is the positive equivalent of this one.
More precisely: (fraction >= 0 ? this : -fraction)
The returned fraction is not reduced.
this if it is positive, or a new positive fraction instance with the opposite signed
numeratorpublic Fraction pow(int power)
Gets a fraction that is raised to the passed in power.
The returned fraction is in reduced form.
power - the power to raise the fraction tothis if the power is one, ONE if the power is zero (even if the fraction equals
ZERO) or a new fraction instance raised to the appropriate powerArithmeticException - if the resulting numerator or denominator exceeds Integer.MAX_VALUEpublic Fraction add(Fraction fraction)
Adds the value of this fraction to another, returning the result in reduced form. The algorithm follows Knuth, 4.5.1.
fraction - the fraction to add, must not be nullFraction instance with the resulting valuesIllegalArgumentException - if the fraction is nullArithmeticException - if the resulting numerator or denominator exceeds Integer.MAX_VALUEpublic Fraction subtract(Fraction fraction)
Subtracts the value of another fraction from the value of this one, returning the result in reduced form.
fraction - the fraction to subtract, must not be nullFraction instance with the resulting valuesIllegalArgumentException - if the fraction is nullArithmeticException - if the resulting numerator or denominator cannot be represented in an int.public Fraction multipliedBy(Fraction fraction)
Multiplies the value of this fraction by another, returning the result in reduced form.
fraction - the fraction to multiply by, must not be nullFraction instance with the resulting valuesIllegalArgumentException - if the fraction is nullArithmeticException - if the resulting numerator or denominator exceeds Integer.MAX_VALUEpublic Fraction dividedBy(Fraction fraction)
Divide the value of this fraction by another.
the fraction to divide by, must not benullFraction instance with the resulting valuesIllegalArgumentException - if the fraction is nullArithmeticException - if the resulting numerator or denominator exceeds Integer.MAX_VALUEpublic int compareTo(Fraction other)
Compares this object to another based on size.
Note: this class has a natural ordering that is inconsistent with equals, because, for example, equals treats 1/2 and 2/4 as different, whereas compareTo treats them as equal.
compareTo in interface Comparable<Fraction>other - the object to compare toClassCastException - if the object is not a FractionNullPointerException - if the object is nullpublic String toProperString()
Gets the fraction as a proper String in the format X Y/Z.
The format used in 'wholeNumber numerator/denominator'. If the whole number is zero it will be omitted. If the numerator is zero, only the whole number is returned.
String form of the fractionpublic boolean equals(Object obj)
Compares this fraction to another object to test if they are equal.
.To be equal, both values must be equal. Thus 2/4 is not equal to 1/2.
public int hashCode()
Gets a hashCode for the fraction.
Copyright © 2021. All rights reserved.