Search code examples
javanumbersfractions

Is there way to know that fraction is actually whole number?


I use Fraction from apache library

 Fraction fraction = Fraction.getFraction(0, 7, 13);
 fraction = fraction.multiplyBy(Fraction.getFraction(26));
 System.out.print(fraction.getProperWhole());

this code returns 14 - it is expected result

 Fraction fraction = Fraction.getFraction(0, 7, 13);
 fraction = fraction.multiplyBy(Fraction.getFraction(27));
 System.out.print(fraction.getProperWhole());

It outs 14 too but actually it is not exact 14

Is there way to know that fraction is actually whole number


Solution

  • You seem to be using org.apache.commons.lang.math.Fraction. This has a getProperNumerator method which returns 0 if and only if the fraction is equal to an integer.