Solved

double numbers

Posted on 2004-10-05
3
217 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
ID: 12225282
see this for rounding the double
http://www.rgagnon.com/javadetails/java-0016.html
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12225310
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
ID: 12225325
This is because of the nature of floating point numbers, and arithmetic... the .999999998 result is not an error....
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

867 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

16 Experts available now in Live!

Get 1:1 Help Now