如何使用BigDecimal来实现以下代码而不是使用double或float?

时间:2014-11-25 09:14:58

标签: java bigdecimal

我有这段代码

public static double distance(double lat1, double lon1, double lat2, double lon2, char unit) {      
      double theta = lon1 - lon2;
      double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
      dist = Math.acos(dist);
      dist = rad2deg(dist);
      dist = dist * 60 * 1.1515;
      if (unit == 'K') {
        dist = dist * 1.609344;     
      } else if (unit == 'N') {     
        dist = dist * 0.8684;   
      }
      return (dist);
    }

但由于上述代码精度的准确性下降,我必须通过将类型设为BigDecimal而不是lat1,lon1,lat2,lon2的double来改变它。但问题是,如果我使用BigDecimal,我怎样才能实现所有这些操作?
请指导我实现这一点 谢谢:))

1 个答案:

答案 0 :(得分:0)

受到这篇文章的启发:How to convert BigDecimal to Double in Java?

可能转换应该有效吗?

BigDecimal bd; // the value you get
double d = bd.doubleValue(); // The double you want