• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Plz clarify

 
Ankith suresh
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int intNumber = 123456789;
float floatNumber = intNumber;
int x = (int)floatNumber;
System.out.println(intNumber - x);
I m geting result -3 .how?
 
Henry Wong
author
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keep in mind that a floating point number has both a range and precision. While the range of a float can easily hold an int, it may not have the precision to hold all int values accurately.

Anyway, print out the float value -- just to see exactly what value is being held. You will notice that it is slightly off.

Henry
 
Saurabh Verma
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
float floatNumber = intNumber;

After this floatNumber = 1.23456792E8

And when it assignd to x , x = 123456792

Thats why intNumber - x printing -3.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic