File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes float 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 "float" Watch "float" New topic
Author

float

Sunny Kiran
Greenhorn

Joined: Mar 31, 2005
Posts: 10
float f2 = 412.1231623f;

System.out.println("f2 : " + f2 );

Output:
f2 : 412.12317

why so??? why it is getting rounded to 7 instead of being 6?
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
Kiran,
Even i dont know much about the working of this.
I checked into API, it says

System.out.println(<float value> is same as
Float.toString(<float value>

In the method toString() of Float class they have explained various scenarios. Its quite detailed and u need patience to go thru it.
This is what they have told regarding the digits after decimal part

How many digits must be printed for the fractional part of m or a? There must be at least one digit to represent the fractional part, and beyond that as many, but only as many, more digits as are needed to uniquely distinguish the argument value from adjacent values of type float. That is, suppose that x is the exact mathematical value represented by the decimal representation produced by this method for a finite nonzero argument f. Then f must be the float value nearest to x; or, if two float values are equally close to x, then f must be one of them and the least significant bit of the significand of f must be 0.


I didnt understand what they said,
So let someone try and make us understand
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Because a float is a IEEE754 floating-point type.

http://www.xdweb.net/~dibblego/miscellaneous/documents/IEEE754.pdf
http://www.xdweb.net/~dibblego/java/faq/answers.html#q41


Tony Morris
Java Q&A (FAQ, Trivia)
Sagar Kar
Greenhorn

Joined: Sep 03, 2004
Posts: 25
Somewhere I had read that float values are only precise till 9 digits.


Warm regards,<br />Sagar Kar.<br />SCJP 1.4, SCBCD 1.3
ShivShankar
Greenhorn

Joined: Mar 18, 2005
Posts: 12
Hi Kiran,

Print a floating-point number. The string produced by String.valueOf(float) is translated into bytes according to the "platform's default character encoding",and these bytes are written in exactly the manner of the write(int) method.



Shivu<br />(SCJP1.4,SCBCD(In Progress..)<br />We are what we think.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: float
 
Similar Threads
Question on Float.NAN
Float.NaN is equal to Float.NaN
Float Compare - Help
about Float and Double
float equality