2017-04-18 55 views
2

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

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

回答

0

阅读java tutorial on formatting numeric output。具体来说,printf("%.2f", number)是你在找什么。

+0

我尝试过,但没有工作。 :( – Ankit

+0

'double d = 1.23456; System.out.printf(“%。2f”,d);''打印'1,23'对我来说正确 –

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

对不起,这也不起作用,请检查我的代码。 – Ankit

+0

@Ankit检查已更新答案它适用于我 – Sach

+0

兄弟不工作为我。我不知道为什么? – Ankit

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

我试过使用它,但距离甚至没有显示。纬度经度,我将添加什么(yourValue)? – Ankit

+0

任何值或变量,你想格式化为2位小数!格式化然后使用resultString或重命名,如果你想! –

0

使用为您解决问题

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