문제

Isn't it true that if you cast a float number like 1.0012 to an integer, it will become 1?

Then why is it when I write:

(int)(14/13-0.001) 

instead of 1.07592 ~ become 1 it becomes 0 ?

(Java compiled with Eclipse).

도움이 되었습니까?

해결책

It does truncating. For 1.0012 it just removes part on right of decimal point.

In example

(int)(14/13-0.001)

14/13 will be 1 and then it will be converted to double, 1.0-0.001 = 0.999, and after truncating it becomes 0.

다른 팁

14 / 13 is an integer division. Its value is 1.

1 - 0.01 is thus lower than 1.

Casting it to int thus yields 0.

Use 14.0 / 13 - 0.001 or 14d / 13 - 0.001 instead.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top