Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
非常によく似た質問がたくさんありますが、私の質問に完全に答えたものはありませんので、ご容赦ください。
2 つの double がある場合、それらを小数点以下 2 桁に丸め、差が正確に 0.01 であるかどうかを確認する必要があります。
これを行うための最良の方法は何ですか?
他の人は、浮動小数点数の比較の問題を指摘しています。あなたの最善の策は、それぞれに100を掛けてから、整数部分を整数として比較することです:
static bool ExactlyPennyDifference(double d1, double d2) { return Math.Abs((int)Math.Round(d1 * 100) - (int)Math.Round(d2 * 100)) == 1; }