I am trying to round down a double using Big decimal and then comparing to another number For example:
// Parse the string to extract the double and then put it into Big
// decimal
BigDecimal bdAns = BigDecimal.valueOf(Double.parseDouble("3.1419"));
System.out.println(bdAns);
BigDecimal bdCorr = BigDecimal.valueOf(Double.parseDouble("3.142"));
System.out.println(bdCorr);
System.out.println(bdCorr.precision() + " , " + bdAns.precision());
// Round down the value of the bdAns, so that the precision of bdAns and
// bdCorr matches. This code doesnt seem to work.
bdAns = BigDecimal.valueOf(bdAns).setScale(bdCorr.precision(),
BigDecimal.ROUND_UNNECESSARY);
System.out.println(bdAns);
System.out.println(bdAns.compareTo(bdCorr));
The last println is printing -1.But they should be equal as 3.1419 round to 3 places after decimal should be 3.142. Can somebody please tell me what is wrong with the code?
precisionandscaleare not the same thing, it appears you are confusing the two.scaleis the number of digits to the right of the decimal point.precisionis the total number of digits.Change all of your references to
precisiontoscaleand it should work (but you’ll have to pick a rounding mode other thanUNNECESSARY).