In addition to other answers, I would like to answer the question why the cycle ends earlier with the condition i == (d * 10) than with i == (int) (d * 10) .
In the first case, the value of int on the left side == increases to double, so the inequality occurs when the accumulated error in d*10 is either positive or negative (for example, 0.999999 or 1.000001).
In the second case, the right-hand side is truncated to int, so inequality occurs only when the error is negative (for example, 5.999999). Therefore, the first version will fail.
source share