2

What is an easy way to get the integral part of a BigFraction as a BigInteger?

Basically I want the same result that the intValue and longValue methods return but with arbitrary precision.

I also want to avoid rounding so indirect conversion via a BigDecimal is not suitable.

Johan
  • 74,508
  • 24
  • 191
  • 319
finnw
  • 47,861
  • 24
  • 143
  • 221

3 Answers3

3

 

myBigFraction.getNumerator().divide( myBigFraction.getDemoninator() );

?

mob
  • 117,087
  • 18
  • 149
  • 283
  • Unsurprisingly `BigFraction.longValue()` is implemented this way. The surprising part is that there is no `bigIntegerValue()` method. – finnw Jul 08 '10 at 15:02
0

You could try something like this

BigFraction fraction = ...

BigInteger num = fraction.getNumerator();
BigInteger den = fraction.getDenominator();
BigInteger[] divideAndReminder = num.divideAndRemainder(den);

Then finally

BigInteger integralPart = divideAndReminder[0];
Lombo
  • 11,847
  • 2
  • 20
  • 27
0

This should do the trick.

bigInteger = bigFraction.genNumerator().divide(bigFraction.getDenominator());
torak
  • 5,684
  • 21
  • 25