BigDecimalMath { public static BigDecimal roundWithTrailingZeroes(BigDecimal value, MathContext mathContext) { if (value.precision() == mathContext.getPrecision()) { return value; } if (value.signum() == 0) { return BigDecimal.ZERO.setScale(mathContext.getPrecision() - 1); } try { BigDecimal stripped = value.stripTrailingZeros(); int exponentStripped = exponent(stripped); BigDecimal zero; if (exponentStripped < -1) { zero = BigDecimal.ZERO.setScale(mathContext.getPrecision() - exponentStripped); } else { zero = BigDecimal.ZERO.setScale(mathContext.getPrecision() + exponentStripped + 1); } return stripped.add(zero, mathContext); } catch (ArithmeticException ex) { return value.round(mathContext); } } private  BigDecimalMath(); static BigDecimal toBigDecimal(String string); static BigDecimal toBigDecimal(String string, MathContext mathContext); static boolean isIntValue(BigDecimal value); static boolean isLongValue(BigDecimal value); static boolean isDoubleValue(BigDecimal value); static BigDecimal mantissa(BigDecimal value); static int exponent(BigDecimal value); static int significantDigits(BigDecimal value); static BigDecimal integralPart(BigDecimal value); static BigDecimal fractionalPart(BigDecimal value); static BigDecimal round(BigDecimal value, MathContext mathContext); static BigDecimal roundWithTrailingZeroes(BigDecimal value, MathContext mathContext); static BigDecimal reciprocal(BigDecimal x, MathContext mathContext); static BigDecimal factorial(int n); static BigDecimal factorial(BigDecimal x, MathContext mathContext); static BigDecimal gamma(BigDecimal x, MathContext mathContext); static BigDecimal bernoulli(int n, MathContext mathContext); static BigDecimal pow(BigDecimal x, BigDecimal y, MathContext mathContext); static BigDecimal pow(BigDecimal x, long y, MathContext mathContext); static BigDecimal sqrt(BigDecimal x, MathContext mathContext); static BigDecimal root(BigDecimal x, BigDecimal n, MathContext mathContext); static BigDecimal log(BigDecimal x, MathContext mathContext); static BigDecimal log2(BigDecimal x, MathContext mathContext); static BigDecimal log10(BigDecimal x, MathContext mathContext); static BigDecimal pi(MathContext mathContext); static BigDecimal e(MathContext mathContext); static BigDecimal exp(BigDecimal x, MathContext mathContext); static BigDecimal sin(BigDecimal x, MathContext mathContext); static BigDecimal asin(BigDecimal x, MathContext mathContext); static BigDecimal cos(BigDecimal x, MathContext mathContext); static BigDecimal acos(BigDecimal x, MathContext mathContext); static BigDecimal tan(BigDecimal x, MathContext mathContext); static BigDecimal atan(BigDecimal x, MathContext mathContext); static BigDecimal atan2(BigDecimal y, BigDecimal x, MathContext mathContext); static BigDecimal cot(BigDecimal x, MathContext mathContext); static BigDecimal acot(BigDecimal x, MathContext mathContext); static BigDecimal sinh(BigDecimal x, MathContext mathContext); static BigDecimal cosh(BigDecimal x, MathContext mathContext); static BigDecimal tanh(BigDecimal x, MathContext mathContext); static BigDecimal coth(BigDecimal x, MathContext mathContext); static BigDecimal asinh(BigDecimal x, MathContext mathContext); static BigDecimal acosh(BigDecimal x, MathContext mathContext); static BigDecimal atanh(BigDecimal x, MathContext mathContext); static BigDecimal acoth(BigDecimal x, MathContext mathContext);  }
@Test public void testRoundWithTrailingZeroes() { MathContext mc = new MathContext(5); assertEquals("0.0000", BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("0"), mc).toString()); assertEquals("0.0000", BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("0.00000000000"), mc).toString()); assertEquals("1.2345", BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("1.2345"), mc).toString()); assertEquals("123.45", BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("123.45"), mc).toString()); assertEquals("0.0012345", BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("0.0012345"), mc).toString()); assertEquals("1.2346", BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("1.234567"), mc).toString()); assertEquals("1.2300", BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("1.23"), mc).toString()); assertEquals("1.2300", BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("1.230000"), mc).toString()); assertEquals("123.46", BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("123.4567"), mc).toString()); assertEquals("0.0012346", BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("0.001234567"), mc).toString()); assertEquals("0.0012300", BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("0.00123"), mc).toString()); assertEquals("1.2346E+100", BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("1.234567E+100"), mc).toString()); assertEquals("1.2346E-100", BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("1.234567E-100"), mc).toString()); assertEquals("1.2300E+100", BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("1.23E+100"), mc).toString()); assertEquals("1.2300E-100", BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("1.23E-100"), mc).toString()); assertEquals("1.2346E+999999999", BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("1.234567E+999999999"), mc).toString()); assertEquals("1.2346E-999999999", BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("1.234567E-999999999"), mc).toString()); assertEquals("1.2300E+999999999", BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("1.23E+999999999"), mc).toString()); assertEquals("1.2300E-999999999", BigDecimalMath.roundWithTrailingZeroes(new BigDecimal("1.23E-999999999"), mc).toString()); }