简体   繁体   English

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

[英]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.

相关问题 初学者使用 BigDecimal 而不是 Double? - Using BigDecimal as a beginner instead of Double? 为什么 java.math.BigDecimal.valueOf(double) 确实使用 new BigDecimal(String),而不是 new BigDecimal(double) - why java.math.BigDecimal.valueOf(double) does use new BigDecimal(String), instead of new BigDecimal(double) 将位转换为双精度后,如何在不使用 BigDecimal 的情况下存储实际的浮点/双精度值? - After converting bits to Double, how to store actual float/double value without using BigDecimal? 如何知道 BigDecimal 是否可以准确地转换为 float 或 double? - How to know if a BigDecimal can exactly convert to float or double? 在Java中使用BigDecimal而不是使用double进行权衡 - The trade-off that comes with using BigDecimal instead of double in Java 为什么这段代码将值转换为双精度而不是浮点数? - Why this code convert value to double instead to float? 无法取消引用浮动-使用BigDecimal - Float cannot be dereferenced - using BigDecimal Java Hibernate条件平均值:BigDecimal而不是Double - Java Hibernate Criteria Average: BigDecimal instead of Double 如何在Java中将BigDecimal转换为Double? - How to convert BigDecimal to Double in Java? 如何在以下代码中实现线程安全 - How do I achieve thread safety in following code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM