public final class Fraction extends Number implements Comparable<Fraction>
Fraction 是一个 Number 实现,
可以准确地存储分数。
此类是不可变的,并且与大多数接受
Number 的方法兼容。
请注意,此类适用于常见用例,它是基于 int 的, 因此容易受到各种溢出问题的影响。对于基于 BigInteger 的等效类, 请参见 Commons Math 库中的 BigFraction 类。
此类来自:Apache Commons Lang3
| Modifier and Type | Field and Description |
|---|---|
static Fraction |
ONE
Fraction 表示 1. |
static Fraction |
ZERO
Fraction 表示 0. |
| Constructor and Description |
|---|
Fraction(int numerator,
int denominator)
使用分数的两个部分构造一个
Fraction 实例,例如 Y/Z。 |
| Modifier and Type | Method and Description |
|---|---|
Fraction |
abs()
获取与此分数等值的正分数。
|
Fraction |
add(Fraction fraction)
将此分数与另一个分数相加,并返回约简后的结果。
|
int |
compareTo(Fraction other) |
Fraction |
divideBy(Fraction fraction)
将此分数除以另一个分数。
|
double |
doubleValue() |
boolean |
equals(Object obj) |
float |
floatValue() |
int |
getDenominator()
获取分数的分母部分。
|
int |
getNumerator()
获取分数的分子部分。
|
int |
getProperNumerator()
获取真分数的分子部分,始终为正数。
|
int |
getProperWhole()
获取真分数的整数部分,包括符号。
|
int |
hashCode() |
int |
intValue() |
Fraction |
invert()
获取此分数的倒数 (1/fraction)。
|
long |
longValue() |
Fraction |
multiplyBy(Fraction fraction)
将此分数与另一个分数相乘,并返回约简后的结果。
|
Fraction |
negate()
获取此分数的负数 (-fraction)。
|
static Fraction |
of(double value)
从
double 值创建一个 Fraction 实例。 |
static Fraction |
of(int numerator,
int denominator)
使用分数的两个部分 Y/Z 创建一个
Fraction 实例。 |
static Fraction |
of(int whole,
int numerator,
int denominator)
使用分数的三个部分 X Y/Z 创建一个
Fraction 实例。 |
static Fraction |
of(String str)
从
String 创建一个 Fraction。 |
static Fraction |
ofReduced(int numerator,
int denominator)
使用分数的两个部分 Y/Z 创建一个简化后的
Fraction 实例。 |
Fraction |
pow(int power)
获取此分数的指定幂次。
|
Fraction |
reduce()
将分数约简为分子和分母的最小值,并返回结果。
|
Fraction |
subtract(Fraction fraction)
从此分数中减去另一个分数的值,并返回约简后的结果。
|
String |
toProperString()
获取此分数的真分数形式的字符串表示,格式为 X Y/Z。
|
String |
toString() |
byteValue, shortValuepublic Fraction(int numerator,
int denominator)
Fraction 实例,例如 Y/Z。numerator - 分子,例如在“七分之三”中的三denominator - 分母,例如在“七分之三”中的七public static Fraction of(double value)
value - 要转换的 double 值ArithmeticException - 如果 |value| > Integer.MAX_VALUE 或 value = NaNArithmeticException - 如果计算出的分母为 zeroArithmeticException - 如果算法不收敛public static Fraction of(int numerator, int denominator)
Fraction 实例。
任何负号都会被解析到分子上。
numerator - 分子,例如 '3/7' 中的 3denominator - 分母,例如 '3/7' 中的 7ArithmeticException - 如果分母为 zero 或分母为 negative 且分子为 Integer#MIN_VALUEpublic static Fraction of(int whole, int numerator, int denominator)
Fraction 实例。
负号必须传递到整数部分。
whole - 整数部分,例如 '一又七分之三' 中的 一numerator - 分子,例如 '一又七分之三' 中的 三denominator - 分母,例如 '一又七分之三' 中的 七ArithmeticException - 如果分母为 zeroArithmeticException - 如果分母为负数ArithmeticException - 如果分子为负数ArithmeticException - 如果结果分子超过 Integer.MAX_VALUEpublic static Fraction of(String str)
str - 要解析的字符串,必须不为 nullFraction 实例NullPointerException - 如果字符串为 nullNumberFormatException - 如果数字格式无效public static Fraction ofReduced(int numerator, int denominator)
Fraction 实例。
例如,如果输入参数表示 2/4,则创建的分数将是 1/2。
任何负号都会被解析到分子上。
numerator - 分子,例如 '3/7' 中的 3denominator - 分母,例如 '3/7' 中的 7ArithmeticException - 如果分母为 zeropublic Fraction abs()
更精确地说:(fraction >= 0 ? this : -fraction)
返回的分数不会被约简。
this;否则返回一个新的正分数实例,其分子符号相反public Fraction add(Fraction fraction)
fraction - 要添加的分数,不能为空Fraction 实例NullPointerException - 如果传入的分数为 nullArithmeticException - 如果结果的分子或分母超出 Integer.MAX_VALUEpublic Fraction divideBy(Fraction fraction)
fraction - 要除以的分数,不能为空Fraction 实例NullPointerException - 如果传入的分数为 nullArithmeticException - 如果要除以的分数为零ArithmeticException - 如果结果的分子或分母超出 Integer.MAX_VALUEpublic int getDenominator()
public int getNumerator()
此方法可能返回一个大于分母的值,即一个假分数,例如 7/4 中的七。
public int getProperNumerator()
一个假分数 7/4 可以分解为真分数 1 3/4。 此方法返回真分数中的 3。
如果分数为负数,例如 -7/4,可以分解为 -1 3/4, 因此此方法返回正数的真分数分子,即 3。
public int getProperWhole()
一个假分数 7/4 可以分解为真分数 1 3/4。 此方法返回真分数中的 1。
如果分数为负数,例如 -7/4,可以分解为 -1 3/4, 因此此方法返回真分数的整数部分 -1。
public Fraction invert()
返回的分数不会被约简。
ArithmeticException - 如果分数表示零public Fraction multiplyBy(Fraction fraction)
fraction - 要乘以的分数,不能为空Fraction 实例NullPointerException - 如果传入的分数为 nullArithmeticException - 如果结果的分子或分母超出 Integer.MAX_VALUEpublic Fraction negate()
返回的分数不会被约简。
public Fraction pow(int power)
返回的分数已约简。
power - 要将分数提升到的幂次this;如果幂次为零(即使分数等于零),则返回 ONE;
否则返回一个新的分数实例,提升到相应的幂次ArithmeticException - 如果结果的分子或分母超出 Integer.MAX_VALUEpublic Fraction reduce()
例如,如果此分数表示 2/4,则结果将是 1/2。
public Fraction subtract(Fraction fraction)
fraction - 要减去的分数,不能为空Fraction 实例NullPointerException - 如果传入的分数为 nullArithmeticException - 如果结果的分子或分母无法表示为 intpublic String toProperString()
使用的格式为 '整数部分 分子/分母'。 如果整数部分为零,则省略整数部分。如果分子为零,则仅返回整数部分。
public double doubleValue()
doubleValue in class Numberpublic float floatValue()
floatValue in class Numberpublic int compareTo(Fraction other)
compareTo in interface Comparable<Fraction>Copyright © 2025. All rights reserved.