Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

double numbers

Posted on 2004-10-05
3
Medium Priority
?
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
armoghan earned 750 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

721 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