This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes query about double addition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "query about double addition" Watch "query about double addition" New topic
Author

query about double addition

Alim Atar
Ranch Hand

Joined: Oct 06, 2006
Posts: 102

Hi,

I have a question about double addition in java 6. I tired following

double d1= 2.748 + 9.160;
System.out.println ( d1 );

I got following output 11.908000000000001 instead of 11.908 exactly.

Can anyone explain why it is so?


regards,
alim


Regards,
Alim

SCEA/OCMJEA Part1,SCBCD1.5,SCWCD1.4,SCJP1.4,SQL(Z007)
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
To get the exact output use BigDecimal.


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
probably this will help you to understand why the output is like that.


SCJP 1.6 96%
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: query about double addition
 
Similar Threads
Double Boolean Operations
waht is -0.0?
Floating-point Operations
Double / Float Doubt.
casting problem