It's not a secret anymore!*
The moose likes Beginning Java and the fly likes formatting using printf Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "formatting using printf" Watch "formatting using printf" New topic
Author

formatting using printf

P Teng
Greenhorn

Joined: Nov 20, 2009
Posts: 16
Can some one please explain the output caused by line 1



output is below. What causes 559998 to appear after the . instead of just 56
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

The way floating point numbers are handled on computers. In short, just like you can't represent 1/3 exactly in decimal (0.3333 is an approximation), you can't represent 0.56 or even 0.1 exactly in binary. See this.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
GeeCON Prague 2014
 
subject: formatting using printf