十进制后无法仅显示1或空数字

时间:2017-04-18 08:28:43

标签: java android google-maps distance latitude-longitude

我试图以km为单位显示距离。但是,小数点后有12位数。我想要小数点后的空或2位数。它显示4.539224078139681公里,我只想要4.53或4公里。怎么做? 我查了很多文章,但无法得到答案。

  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;
}  

5 个答案:

答案 0 :(得分:0)

阅读java tutorial on formatting numeric output。具体而言,printf("%.2f", number)是您正在寻找的。

答案 1 :(得分:0)

使用此方法

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;
}

输入:round(4.539224078139681,2) 输出:4.54

您可以在距离计算方法中使用它并使其返回

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

答案 2 :(得分:0)

检查此答案

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));

    }


}

输出:

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

答案 3 :(得分:0)

您可以根据需要使用DecimalFormat格式化输出!

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);

答案 4 :(得分:0)

使用以下代码解决您的问题

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