Solved

double numbers

Posted on 2004-10-05
3
216 Views
Last Modified: 2008-03-06
Hi dear experts;

I have two double numbers like following
double A= 0.6;
double B=0.5;

when i try to subtract B from A like following
system.Out.print(A-B);
it gives me  0.0999999998  but i want the result to be  0.10 or 0.1
how can i do this ?  i try ceil floor and round methods of Math class but they didint work
it is an error of java ?
because  when i try to 0.8-06= 0.200000000000004  i want to see result like 0.2
Sincerely,



0
Comment
Question by:elmakarge
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
armoghan earned 250 total points
Comment Utility
see this for rounding the double
http://www.rgagnon.com/javadetails/java-0016.html
0
 
LVL 18

Expert Comment

by:armoghan
Comment Utility
public static final double roundDouble(double d, int places)
{        
return Math.round(d * Math.pow(10, (double) places)) / Math.pow(10,            (double) places);    
}
0
 
LVL 11

Expert Comment

by:cjjclifford
Comment Utility
This is because of the nature of floating point numbers, and arithmetic... the .999999998 result is not an error....
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now