I am trying to convert the user’s input from the EditText into a Big Decimal. However, I am having problems doing that. My result is not converted. For example, after the computation is done, the number will be 12.345678. After calling method round(), the rounded value is still the same. Am I doing something wrong? I’m dealing with money
.java
double totalPrice = Double.parseDouble(price.getText().toString());
int position = spinner.getSelectedItemPosition();
name = name1.getText().toString();
if(position == 0)
{
totalPrice = totalPrice * 1.07;
System.out.println(totalPrice);
}
else
{
totalPrice = (totalPrice * 1.1)*1.07;
System.out.println(totalPrice);
}
round(totalPrice, 2, BigDecimal.ROUND_HALF_UP);
}
}
public static double round(double unrounded, int precision, int roundingMode)
{
BigDecimal bd = new BigDecimal(unrounded);
BigDecimal rounded = bd.setScale(precision, roundingMode);
return rounded.doubleValue();
}
You’re not using the result of
round. You should at least be doing:However, you shouldn’t be using
doublefor currency values to start with. UseBigDecimalthroughout – otherwise you may well find you get unexpected results due to the way binary floating point works.