开发者

Is it possible to tell the Fraction class not to reduce to the lowest format

I use Fraction for representing fractions, but the problem is it reduces to the lowest form immediately on providing inputs to the constructor. Is there a way to prevent this behavior?

e.g. Fraction f1 = new Fraction (1, 3) == 1/3
Fraction f2 = new Fra开发者_如何学Pythonction (2, 4) == gets reduced to 1/2 but I would rather want it as 2/4

How to do this?


Instead of using org.apache.commons.lang.math.Fraction.getReducedFraction(int, int) use org.apache.commons.lang.math.Fraction.getFraction(int, int)

System.out.println("fraction: " + Fraction.getFraction(2, 4));
System.out.println("reduced:  " + Fraction.getReducedFraction(2, 4));

results in:

fraction: 2/4
reduced:  1/2


Not possible, unless you switch to Commons-Lang Fraction instead of Commons-Math.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜