I have the following function that is used to round a double value in Java:
public static double round(double d, int decimalPlace) {
BigDecimal bd = new BigDecimal(Double.toString(d));
bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
return bd.doubleValue();
}
As input, this function is receiving these values:
double d = 7.3149999999999995;
int decimalPlace = 2
But, when the function returns, the value returned is 7.31, instead of 7.32. I searched on the docs to see why the bd.SetScale is with that behavior, but with no success.
Does anybody could me explain why is this happening? Thanks a lot!!