Solved

double numbers

Posted on 2004-10-05
3
219 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how do i compare an object based on two fields 6 53
diffSum example 4 36
iterator/ListIterator approach 17 25
maven module vs maven project 3 22
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

776 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