BigRational implements Comparable<BigRational> { public BigRational withPrecision(int precision) { return valueOf(toBigDecimal(new MathContext(precision))); } private  BigRational(int value); private  BigRational(BigDecimal num, BigDecimal denom); BigInteger getNumeratorBigInteger(); BigDecimal getNumerator(); BigInteger getDenominatorBigInteger(); BigDecimal getDenominator(); BigRational reduce(); BigRational integerPart(); BigRational fractionPart(); BigRational negate(); BigRational reciprocal(); BigRational abs(); int signum(); BigRational increment(); BigRational decrement(); BigRational add(BigRational value); BigRational add(BigInteger value); BigRational add(int value); BigRational subtract(BigRational value); BigRational subtract(BigInteger value); BigRational subtract(int value); BigRational multiply(BigRational value); BigRational multiply(BigInteger value); BigRational multiply(int value); BigRational divide(BigRational value); BigRational divide(BigInteger value); BigRational divide(int value); boolean isZero(); boolean isInteger(); BigRational pow(int exponent); BigRational withPrecision(int precision); BigRational withScale(int scale); double toDouble(); float toFloat(); BigDecimal toBigDecimal(); BigDecimal toBigDecimal(MathContext mc); @Override int compareTo(BigRational other); @Override int hashCode(); @Override boolean equals(Object obj); @Override String toString(); String toPlainString(); String toRationalString(); String toIntegerRationalString(); static BigRational valueOf(int value); static BigRational valueOf(int numerator, int denominator); static BigRational valueOf(int integer, int fractionNumerator, int fractionDenominator); static BigRational valueOf(BigInteger numerator, BigInteger denominator); static BigRational valueOf(BigInteger value); static BigRational valueOf(double value); static BigRational valueOf(BigDecimal value); static BigRational valueOf(String string); static BigRational valueOf(boolean positive, String integerPart, String fractionPart, String fractionRepeatPart, String exponentPart); static BigRational valueOf(BigDecimal numerator, BigDecimal denominator); static BigRational min(BigRational... values); static BigRational max(BigRational... values); static BigRational bernoulli(int n); static final BigRational ZERO; static final BigRational ONE; static final BigRational TWO; static final BigRational TEN; }
@Test public void testWithPrecision() { assertEquals("123.456", valueOf(123.456).withPrecision(7).toString()); assertEquals("123.456", valueOf(123.456).withPrecision(6).toString()); assertEquals("123.46", valueOf(123.456).withPrecision(5).toString()); assertEquals("123.5", valueOf(123.456).withPrecision(4).toString()); assertEquals("123", valueOf(123.456).withPrecision(3).toString()); assertEquals("120", valueOf(123.456).withPrecision(2).toString()); assertEquals("100", valueOf(123.456).withPrecision(1).toString()); assertEquals("123.456", valueOf(123.456).withPrecision(0).toString()); }