I found many answer speaking about converting String to Double (with two decimals) but I'm facing a weird case. When printing the value no problem, it is right. But when I do calculations the program acts weird.
I have this code:
String str = "21.90";
I want to convert this string into a double. I tried many solution but none works properly.
double amount = Double.valueOf(str);
or
double amount = Double.parseDouble(str);
or
try {
amount = DecimalFormat.getNumberInstance().parse(str).doubleValue();
}
catch (ParseException e){
// error
}
I've tried also with rounding methods like:
double roundOff = Math.round(amount * 100.0) / 100.0;
Number is converted in "21.9" but when I do, for example:
System.out.println(number - 21.8) = 0.09999999999999787
I don't understand why it's doing this.
Thanks in advance for your help.