基本的に、私は435の整数に変換される4.35 * 100 = 435.0のときに、以下のコードが434を出力する理由を理解できません。Java丸めオフ問題
これについての簡単な説明は何ですか?これは頻繁に盛り上がる問題ですか?
どうすればこの問題を回避できますか?
public class RoundOffDemo {
public static void main(String[] args) {
double price = 4.35;
int cents = (int) (100 * price); // Should be 435
System.out.println(cents); // Prints 434!
}
}
[浮動小数点演算がJavaで正確な結果を生成しない]の可能性重複(http://stackoverflow.com/questions/1661273/floating-point-arithmetic-not- production-exact-results-in-java)を使用します。そして少なくとも100の他の質問。 – dan04