Result of casting double to int is wrong

前端 未结 1 774
终归单人心
终归单人心 2021-01-06 02:37

There seems to be some kind of obscure rounding error when I run the following code:

int roundedTotal = (int)(PriorityJob * 100.0);

Initial

相关标签:
1条回答
  • 2021-01-06 02:55

    Just about every modern computer uses a binary representation for floating-point numbers.

    Just as 1/3 = 0.33333333... can't be represented exactly as a decimal fraction, so 1/10 (and hence most non-integer decimal values, including 1.4) can't be represented exactly as a binary fraction. It will instead be represented by the nearest representable value, which may be slightly more or less than the "true" value.

    You might want to round to the nearest integer instead: (int)(PriorityJob * 100.0 + 0.5)

    0 讨论(0)
提交回复
热议问题