Prev Class | Next Class | Frames | No Frames |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
Number
org.apache.commons.lang.math.Fraction
public final class Fraction
extends Number
implements Serializable, Comparable
Fraction
is a Number
implementation that
stores fractions accurately.
This class is immutable, and interoperable with most methods that accept
a Number
.
Field Summary | |
static Fraction |
|
static Fraction |
|
static Fraction |
|
static Fraction |
|
static Fraction |
|
static Fraction |
|
static Fraction |
|
static Fraction |
|
static Fraction |
|
static Fraction |
|
static Fraction |
|
static Fraction |
|
Method Summary | |
Fraction |
|
Fraction | |
int |
|
Fraction | |
double |
|
boolean |
|
float |
|
int |
|
static Fraction |
|
static Fraction |
|
static Fraction |
|
static Fraction |
|
int |
|
int |
|
int |
|
static Fraction |
|
int |
|
int |
|
Fraction |
|
long |
|
Fraction |
|
Fraction |
|
Fraction |
|
Fraction |
|
Fraction | |
String |
|
String |
|
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.
- Returns:
this
if it is positive, or a new positive fraction instance with the opposite signed numerator
public 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.
- Parameters:
fraction
- the fraction to add, must not benull
- Returns:
- a
Fraction
instance with the resulting values
public int compareTo(Object object)
Compares this object to another based on size.
- Parameters:
object
- the object to compare to
- Returns:
- -1 if this is less, 0 if equal, +1 if greater
public Fraction divideBy(Fraction fraction)
Divide the value of this fraction by another.
- Parameters:
fraction
- the fraction to divide by, must not benull
- Returns:
- a
Fraction
instance with the resulting values
public double doubleValue()
Gets the fraction as adouble
. This calculates the fraction as the numerator divided by denominator.
- Returns:
- the fraction as a
double
public 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.
- Parameters:
obj
- the reference object with which to compare
- Returns:
true
if this object is equal
public float floatValue()
Gets the fraction as afloat
. This calculates the fraction as the numerator divided by denominator.
- Returns:
- the fraction as a
float
public int getDenominator()
Gets the denominator part of the fraction.
- Returns:
- the denominator fraction part
public static Fraction getFraction(String str)
Creates a Fraction from aString
. The formats accepted are:and a .
double
String containing a dot- 'X Y/Z'
- 'Y/Z'
- 'X' (a simple whole number)
- Parameters:
str
- the string to parse, must not benull
- Returns:
- the new
Fraction
instance
public static Fraction getFraction(double value)
Creates aFraction
instance from adouble
value. This method uses the continued fraction algorithm, computing a maximum of 25 convergents and bounding the denominator by 10,000.
- Parameters:
value
- the double value to convert
- Returns:
- a new fraction instance that is close to the value
public static Fraction getFraction(int numerator, int denominator)
Creates aFraction
instance with the 2 parts of a fraction Y/Z. Any negative signs are resolved to be on the numerator.
- Parameters:
numerator
- the numerator, for example the three in 'three sevenths'denominator
- the denominator, for example the seven in 'three sevenths'
- Returns:
- a new fraction instance
public static Fraction getFraction(int whole, int numerator, int denominator)
Creates aFraction
instance with the 3 parts of a fraction X Y/Z. The negative sign must be passed in on the whole number part.
- Parameters:
whole
- the whole number, for example the one in 'one and three sevenths'numerator
- the numerator, for example the three in 'one and three sevenths'denominator
- the denominator, for example the seven in 'one and three sevenths'
- Returns:
- a new fraction instance
public int getNumerator()
Gets 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.
- Returns:
- the numerator fraction part
public int getProperNumerator()
Gets 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.
- Returns:
- the numerator fraction part of a proper fraction, always positive
public int getProperWhole()
Gets 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.
- Returns:
- the whole fraction part of a proper fraction, that includes the sign
public static Fraction getReducedFraction(int numerator, int denominator)
Creates aFraction
instance with the 2 parts of a fraction Y/Z. Any negative signs are resolved to be on the numerator.
- Parameters:
numerator
- the numerator, for example the three in 'three sevenths'denominator
- the denominator, for example the seven in 'three sevenths'
- Returns:
- a new fraction instance, with the numerator and denominator reduced
public int hashCode()
Gets a hashCode for the fraction.
- Returns:
- a hash code value for this object
public int intValue()
Gets the fraction as anint
. This returns the whole number part of the fraction.
- Returns:
- the whole number fraction part
public Fraction invert()
Gets a fraction that is the inverse (1/fraction) of this one. The returned fraction is not reduced.
- Returns:
- a new fraction instance with the numerator and denominator inverted.
public long longValue()
Gets the fraction as along
. This returns the whole number part of the fraction.
- Returns:
- the whole number fraction part
public Fraction multiplyBy(Fraction fraction)
Multiplies the value of this fraction by another, returning the result in reduced form.
- Parameters:
fraction
- the fraction to multiply by, must not benull
- Returns:
- a
Fraction
instance with the resulting values
public Fraction negate()
Gets a fraction that is the negative (-fraction) of this one. The returned fraction is not reduced.
- Returns:
- a new fraction instance with the opposite signed numerator
public Fraction pow(int power)
Gets a fraction that is raised to the passed in power. The returned fraction is in reduced form.
- Parameters:
power
- the power to raise the fraction to
- Returns:
this
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 power
public Fraction reduce()
Reduce the fraction to the smallest values for the numerator and denominator, returning the result..
- Returns:
- a new reduce fraction instance, or this if no simplification possible
public Fraction subtract(Fraction fraction)
Subtracts the value of another fraction from the value of this one, returning the result in reduced form.
- Parameters:
fraction
- the fraction to subtract, must not benull
- Returns:
- a
Fraction
instance with the resulting values
public String toProperString()
Gets the fraction as a properString
in the format X Y/Z. The format used in 'wholeNumber numerator/denominator'. If the whole number is zero it will be ommitted. If the numerator is zero, only the whole number is returned.
- Returns:
- a
String
form of the fraction
public String toString()
Gets the fraction as aString
. The format used is 'numerator/denominator' always.
- Returns:
- a
String
form of the fraction