Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Double result -- I was surprised 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 "Double result -- I was surprised" Watch "Double result -- I was surprised" New topic

Double result -- I was surprised

Rajiv Mellon

Joined: Feb 28, 2006
Posts: 1
Hi everyone

When I executed the following code I was surprise to see the result.I got the output 'a' but I thought logically it should be 'b'. Why this is happening I just multiplied two number which is infact double. I would appreciate any help.

Thank you

public class FirstClass {

public static void main(String[] args) {
FirstClass fr = new FirstClass();

public void test(){
double inp = 178.5030;
inp = 178.5030 * 10000;
System.out.println("output --->"+inp);

a)output --->1785029.9999999998
b)output --->1785030.00
[ February 28, 2006: Message edited by: Rajiv Mellon ]
Henry Wong

Joined: Sep 28, 2004
Posts: 20521

See point 23 of the Javaranch Beginner's FAQ for more information.

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I agree. Here's the link:
subject: Double result -- I was surprised
It's not a secret anymore!