[英]How to use BigDecimal to achieve following code instead of using double or float?
I have this code 我有这个代码
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); }
But Due to loss of accuracy in precision in the above code I have to change it by giving type as BigDecimal instead of double for lat1, lon1, lat2, lon2. 但是由于上述代码中精度的准确性下降,我不得不通过将类型指定为BigDecimal而不是lat1,lon1,lat2,lon2的double来更改它。 But problem is if I use BigDecimal how can I achieve all those operations?
但是问题是,如果我使用BigDecimal,如何实现所有这些操作?
Please guide me in achieving this 请指导我实现这一目标
Thank you :) 谢谢 :)
Inspired from this post: How to convert BigDecimal to Double in Java? 从这篇文章中得到启发: 如何在Java中将BigDecimal转换为Double?
May be a conversion should work? 可能应该进行转换?
BigDecimal bd; // the value you get
double d = bd.doubleValue(); // The double you want
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.