File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Plz clarify Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Plz clarify" Watch "Plz clarify" New topic

Plz clarify

Ankith suresh
Ranch Hand

Joined: Jun 07, 2007
Posts: 42
int intNumber = 123456789;
float floatNumber = intNumber;
int x = (int)floatNumber;
System.out.println(intNumber - x);
I m geting result -3 .how?
Henry Wong

Joined: Sep 28, 2004
Posts: 20535

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.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Saurabh Verma
Ranch Hand

Joined: Aug 13, 2007
Posts: 38
float floatNumber = intNumber;

After this floatNumber = 1.23456792E8

And when it assignd to x , x = 123456792

Thats why intNumber - x printing -3.
I agree. Here's the link:
subject: Plz clarify
It's not a secret anymore!