How to get two value after the decimal for the result of distance

257 Views Asked by At

To calculate the distance between two places;with the two lat and longi using this link too Calculate distance in meters when you know longitude and latitude in java

i got my result as metre like 2.4737676E-5,its a float value too.

i want to take as first two values only after decimal.But using the decimal format and string format also i couldn't get my expected result like(2.47)..its coming as 0.00 only.

>  float meanwhiledist=distFrom(latti,longii,curlat,curlongi);
>                  DecimalFormat df2 = new DecimalFormat( "#.00"); 
>                  double dd2dec = new Double(df2.format(meanwhiledist));

                         or

           Double dd = Double.valueOf(meanwhiledist);
           String angleFormated = String.format("%.2f", dd);

if i print angleformated and dd2dec my result coming as 0.00        
1

There are 1 best solutions below

6
On

The value of 2.4737676E-5 seems to be less than you expect:

    float ff= 2.4737676E-5f;
    System.out.println("float value: "+ff);
    Double dd=Double.valueOf(ff);
    System.out.println("double value: "+dd);
    BigDecimal test=new BigDecimal(dd);
    System.out.println("big decimal value: "+test);
    String angleFormatted=test.toString();
    System.out.println("String value: "+angleFormatted);

Output:

 float value: 2.4737676E-5
 double value: 2.4737675630603917E-5
 big decimal value: 0.000024737675630603916943073272705078125
 String value: 0.000024737675630603916943073272705078125

The output you get is actually correct since it returns the first 2 decimal places from 0.000.....