【问题标题】:unable to show only 1 or null digit after decimal无法在小数点后仅显示 1 位或空位
【发布时间】:2017-09-14 00:06:01
【问题描述】:

我正在尝试以公里为单位的距离及其显示。但是,小数点后有 12 位数字。我想要小数点后的 null 或 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;
}  

【问题讨论】:

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


    【解决方案1】:

    阅读 java tutorial on formatting numeric output。具体来说,printf("%.2f", number) 就是您要查找的内容。

    【讨论】:

    • double d = 1.23456; System.out.printf("%.2f", d); 为我正确打印 1,23
    【解决方案2】:

    您可以使用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);
    

    【讨论】:

    • 我尝试使用它,但距离甚至没有显示。此外,距离来自纬度经度。我将在 (yourValue) 中添加什么?
    • 任何要格式化为小数点后两位的值或变量!格式化它然后使用 resultString 或重命名,如果你想!
    【解决方案3】:

    使用这个方法

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

        检查这个答案

        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
        

        【讨论】:

        • 对不起。这也行不通。请检查我的代码。
        • @Ankit 用输出检查这个答案
        猜你喜欢
        • 2020-03-14
        • 2013-02-23
        • 1970-01-01
        • 1970-01-01
        • 2011-05-27
        • 1970-01-01
        • 2012-05-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多