double
ほとんどの小数値しか近似できません。これは、期待どおりの結果を得るには、丸めを使用する必要があることを意味します。または、この問題を処理する BigDecimal を使用できます。
double test = 0;
test += 0.71;
test += 0.2;
System.out.printf("%.2f%n", test);
版画
0.91
自分の利益のために
System.out.println("0.71 is actually " + new BigDecimal(0.71));
System.out.println("0.2 is actually " + new BigDecimal(0.2));
System.out.println("0.71+0.2 is actually " + new BigDecimal(0.71 + 0.2));
System.out.println("0.91 is actually " + new BigDecimal(0.91));
System.out.println("0.71+0.2 == 0.91 is " + (0.71 + 0.2 == 0.91));
版画
0.71 is actually 0.70999999999999996447286321199499070644378662109375
0.2 is actually 0.200000000000000011102230246251565404236316680908203125
0.71+0.2 is actually 0.9099999999999999200639422269887290894985198974609375
0.91 is actually 0.91000000000000003108624468950438313186168670654296875
0.71+0.2 == 0.91 is false