开发者

Get the integral part of a BigFraction, as a BigInteger

开发者 https://www.devze.com 2023-01-06 15:52 出处:网络
What is an easy way to get the in开发者_运维技巧tegral part of a BigFraction as a BigInteger? Basically I want the same result that the intValue and longValue methods return but with arbitrary precis

What is an easy way to get the in开发者_运维技巧tegral 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.


 

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

?


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];


This should do the trick.

bigInteger = bigFraction.genNumerator().divide(bigFraction.getDenominator());
0

精彩评论

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