简体   繁体   中英

unable to show only 1 or null digit after decimal

I am trying to get the distance in km and its showing. However, there are 12 digits after decimal points. I want null or 2 digits after decimal. Its showing 4.539224078139681 km and i want only 4.53 or 4 km. how to do it? I checked many articles but unable to get the answer.

  int Radius = 6371;// radius of earth in Km
    double dLat = Math.toRadians(lat2 - lat1);
    double dLon = Math.toRadians(lon2 - lon1);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
            + Math.cos(Math.toRadians(lat1))
            * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
            * Math.sin(dLon / 2);
    double c = 2 * Math.asin(Math.sqrt(a));
    double valueResult = Radius * c;
    double km = valueResult / 1;
    DecimalFormat newFormat = new DecimalFormat("####");
    int kmInDec = Integer.valueOf(newFormat.format(km));
    double meter = valueResult % 1000;
    int meterInDec = Integer.valueOf(newFormat.format(meter));
    Log.i("Radius Value", "" + valueResult + "   KM  " + kmInDec
            + " Meter   " + meterInDec);
    return Radius * c;
}  

Read the java tutorial on formatting numeric output . Specifically, printf("%.2f", number) is what you're looking for.

Use this method

public static double round(double value, int places) {
    if (places < 0) throw new IllegalArgumentException();
    long factor = (long) Math.pow(10, places);
    value = value * factor;
    long tmp = Math.round(value);
    return (double) tmp / factor;
}

Input: round(4.539224078139681,2) Output: 4.54

You could use it inside your distance computation method and make it return

return round((Radius * c),2);

Check this Answer

MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();

    Double lat1, lat2, lon1, lon2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        lat1 = 18.477236;
        lon1 = 73.902837;

        lat2 = 18.528896;
        lon2 = 73.874391;

        Log.e(TAG, "Value is " + cal() + " km");


    }


    private String cal() {

        int Radius = 6371;// radius of earth in Km
        double dLat = Math.toRadians(lat2 - lat1);
        double dLon = Math.toRadians(lon2 - lon1);
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
                + Math.cos(Math.toRadians(lat1))
                * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
                * Math.sin(dLon / 2);
        double c = 2 * Math.asin(Math.sqrt(a));
        double valueResult = Radius * c;
        double km = valueResult / 1;
        DecimalFormat newFormat = new DecimalFormat("####");
        int kmInDec = Integer.valueOf(newFormat.format(km));
        double meter = valueResult % 1000;
        int meterInDec = Integer.valueOf(newFormat.format(meter));
        Log.i("Radius Value", "" + valueResult + "   KM  " + kmInDec
                + " Meter   " + meterInDec);



        return String.format("%.2f", (Radius * c));

    }


}

Output:

04-18 14:49:30.740 3039-3039/com.lab.palaswadi.demo I/Radius Value: 6.4803224518370275   KM  6 Meter   6
04-18 14:49:30.741 3039-3039/com.lab.palaswadi.demo E/MainActivity: Value is 6.48 km

You can use DecimalFormat to format your output as you like!

DecimalFormat formatter = new DecimalFormat("#.##"); 
//if you want show two decimal always the use "#.00". Output actual: 2.00 > ".##" 2.0 >".00" 2.00

String resultString = formatter.format(yourValue);

Use below codes to solve your problem

 double yourNumber = 4.539224078139681;
 double yourAnswer = Math.round(yourNumber * 100.0) / 100.0;
 System.out.println(yourAnswer);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM