Java – calculate difference between two double values exactly

androidjava

I want to calculate the difference between two double values

for example : lat1=12.2345673 and lat2=12.2345672 . here i want result as 0.0000001. this much exactly i am not getting while calculate double res=Double.compare(lat1,lat2) in eclipse. it showing 0.0.
Please specify the exact formula to overcome this

Best Answer

Can u try the following code ,

     double lat1=12.2345673;
     double lat2=12.2345672;
     double dif=lat1-lat2;

     DecimalFormat df = new DecimalFormat("###.#######");
     System.out.println("Diff Val  : "+df.format(dif));

Output : Diff Val : 0.0000001