-1
     private void findLatLongDistance(double prelat,double prelon,double lat,double lon) {
    // TODO Auto-generated method stub
    try{
        double prelatval=prelat;
        double prelongval=prelon;
        double curlat=lat;
        double curlon=lon;
        Log.w("inside finalatlon...........................","the daya");
        if(prelatval>0.0 && prelongval>0.0 && lat>0.0 &&  lat>0.0 && gpsdataElements.Speed>0.0){
            float distance2 = getDistance(prelatval,prelongval,curlat,curlon);
            odometer_sum= (distance2/1000 );
            // for maximum value after decimal
            //df.setMaximumFractionDigits(0);

            //rounding the km digits after decimal
            Math.round(distance2);

            //for minimum distance after decimal
            df.setMinimumFractionDigits(2);
            df.format(odometer_sum);
            gpsdataElements.Distance = gpsdataElements.Distance + odometer_sum;
        }
    }catch(Exception e){
        e.printStackTrace();
    }
}

here is my code. I want distance(i.e km)value digits after decimal is two digits. Example i want km value in 1.54km not like 1.4568923km. How to get like this.I tried a lot for that but i din't got any possible solution. Any one know please help me.

Kara
  • 6,115
  • 16
  • 50
  • 57
Ramesh
  • 35
  • 3
  • 9

6 Answers6

2

In more simple ways you can do this by using this :

double roundOff = Math.round(yourValue * 100.0) / 100.0;

Otherwise you can also do this as

String.format("%.2f", d)
Developer
  • 6,292
  • 19
  • 55
  • 115
1
DecimalFormat formatter = new DecimalFormat(".##");
String s = formatter.format(value);
mleczey
  • 513
  • 7
  • 16
0

use this code to format text in two digits after decimal.

double i2=i/60000;
tv.setText(new DecimalFormat("##.##").format(i2));
Shivang Trivedi
  • 2,182
  • 1
  • 20
  • 26
0
double roundof = Math.round(a * 100.0) / 100.0;

output:roundoff(eg:12).oo

12.00

Ansar
  • 364
  • 3
  • 16
0

Something like this :

String.format("%.2f", distance);
Harish Godara
  • 2,388
  • 1
  • 14
  • 28
0

Make function like this pass the value as per you requirment and return value as per your requirment first function will take float value and after decimal it put 3 value as per argument if you want to change decimal value then you can change 3,2,4 etc... Second function will take String value and round it in 3 decimal point:

public static BigDecimal round(float d) {
    BigDecimal bd = new BigDecimal(Float.toString(d));
    bd = bd.setScale(3, BigDecimal.ROUND_HALF_UP);       
    return bd;
}
public static BigDecimal round(String d) {
   BigDecimal bd = new BigDecimal(Float.parseFloat(d));
   bd = bd.setScale(3, BigDecimal.ROUND_HALF_UP);       
   return bd;
}
PankajAndroid
  • 2,689
  • 3
  • 27
  • 41