Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primitive value conversion

This is the snippet of code in Java:

int i = 1234567890;     
float f = i;    
System.out.println(i - (int)f);

Why is that the output is not equal to 0? It performs widening, so it is not supposed to loose data. Then you just truncate the value. Best regards

like image 625
uml Avatar asked Mar 16 '26 01:03

uml


1 Answers

See the difference

 int i = 1234567890;     
 float f = i;    
 System.out.println(i - f);
 System.out.println((int)f);
 System.out.println(f);
 System.out.println(i-(int)f);

Ouput:

0.0

1234567936

1.23456794E9

-46
like image 181
gks Avatar answered Mar 18 '26 14:03

gks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!