I am using BigDecimal for division. I would like the quotient to be rounded to the correct number of significant figures.
For example
@Test
public void testBigDDivision() {
BigDecimal top = new BigDecimal("0.25");
BigDecimal bottom = new BigDecimal("105");
int topSigFig = significantDigits(top);
int botSigFig = significantDigits(bottom);
// evaluates to 2 in this example
int scale = (topSigFig > botSigFig) ? botSigFig : topSigFig;
BigDecimal quot = top.divide(bottom, scale, RoundingMode.HALF_UP);
BigDecimal expected = new BigDecimal("0.0024");
Assert.assertTrue(String.format("Got %s; Expected %s", quot, expected),
expected.compareTo(quot) == 0); // fails "Got 0.00; Expected 0.0024"
}
// stolen from https://stackoverflow.com/a/21443880
public static int significantDigits(BigDecimal input) {
input = input.stripTrailingZeros();
return input.scale() < 0
? input.precision() - input.scale()
: input.precision();
}
What is the correct way to programmatically determine the scale to ensure the quotient has the correct number of significant figures?