Solved

double numbers

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
difference of if loops 23 68
configure dependency in POM for new database 3 43
Problem to Alipay 10 67
activeMQ Queue Messages Enqueued and Messages Dequeued 1 24
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

679 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