0

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        
Karthick M
  • 769
  • 2
  • 11
  • 29
  • you may want to take a look @ map utils library which will help calculate distance in meters. Conversion should not be a problem https://developers.google.com/maps/documentation/android/utility/#spherical – Raghunandan Nov 15 '14 at 04:14

1 Answers1

0

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.....

MihaiC
  • 1,618
  • 1
  • 10
  • 14
  • ,Ok how can i get the expect result as 2.47 @MihaiC – Karthick M Nov 14 '14 at 09:05
  • @KarthickM why do you want it as 2.47 if it's actually smaller than that? 2.47 would not represent the correct value, right? – MihaiC Nov 14 '14 at 09:24
  • Actually that float value is the distance of two places and that one come as metre,i want to change metre to km.so i need to multiply with 0.001;then only i will get as Km. – Karthick M Nov 14 '14 at 09:27
  • @KarthickM sure you can change it to km, but it doesn't change the fact that the result is 0.000024737675630603916943073272705078125 meters and not 2.47 meters. – MihaiC Nov 14 '14 at 09:32
  • so for that what we can do dude?in my question i clearly mention the link which was i followed to find the metres too. – Karthick M Nov 14 '14 at 09:35
  • plz look this link too.http://stackoverflow.com/questions/10734230/rounding-necessary-with-bigdecimal-numbers – Karthick M Nov 14 '14 at 09:41