Double multiplied by 100 and then cast to long is giving wrong value
Posted
by
xyz
on Stack Overflow
See other posts from Stack Overflow
or by xyz
Published on 2012-11-20T06:41:04Z
Indexed on
2012/11/20
11:02 UTC
Read the original article
Hit count: 231
java
|floating-point
I have the following code:
Double i=17.31;
long j=(long) (i*100);
System.out.println(j);
O/P : 1730 //Expected:1731
Double i=17.33;
long j=(long) (i*100);
System.out.println(j);
O/P : 1732 //Expected:1733
Double i=17.32;
long j=(long) (i*100);
System.out.println(j);
O/P : 1732 //Expected:1732{As expected}
Double i=15.33;
long j=(long) (i*100);
System.out.println(j);
O/P : 1533 //Expected:1533{as Expected}
I have tried to Google but unable to find reason.I am sorry if the question is trivial.
© Stack Overflow or respective owner