Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

query about double addition

 
Alim Atar
Ranch Hand
Posts: 102
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get the exact output use BigDecimal.
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
probably this will help you to understand why the output is like that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic