Java float 和 double 数据类型是否适合存储纬度和经度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20657172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:22:18  来源:igfitidea点击:

float and double datatype is good to store latitude and longitude?

javagoogle-mapsgoogle-maps-api-3precisiontype-conversion

提问by ArunRaj

I am storing latitude and longitude at the server side (JAVA Platform).

我在服务器端(JAVA 平台)存储纬度和经度。

To store those values, I am using float and double datatype at the server side. I came to know that float and double are not a recommended primitive datatypes (which is not recommended to use currencies in professional way), because float and double having rounding precision problem.

为了存储这些值,我在服务器端使用了 float 和 double 数据类型。我开始知道 float 和 double 不是推荐的原始数据类型(不建议以专业方式使用货币),因为 float 和 double 具有舍入精度问题

In my case, I am just comparing the stored coordinates (latitude and longitude) at the server side.

就我而言,我只是比较服务器端存储的坐标(纬度和经度)。

Question 1:

问题 1:

Comparing the coordinates with the datatypes (float or double) will make any problem in future?

将坐标与数据类型(浮点数或双精度数)进行比较会在将来产生任何问题吗?

Question 2:

问题2:

Using big decimal is good? or Going with float or double is safer?

使用大十进制好不好?还是使用 float 或 double 更安全?

采纳答案by ThaBomb

It's not a matter of safety, its just a matter of precision. I wouldn't consider floats, but doubles are what i think are ideal here. You just need to see what's the most precision you can get out of a double and see if it fits a regular longitude/latitude value. I think it's more then enough.

这不是安全问题,只是精度问题。我不会考虑花车,但我认为双打是这里的理想选择。您只需要查看可以从 double 中获得的最大精度,并查看它是否适合常规的经度/纬度值。我觉得这就够了。

Else BigDecimal is just a simple backdoor to your problem, use it if you want more precision

否则 BigDecimal 只是您问题的一个简单后门,如果您想要更高的精度,请使用它

回答by Husman

you will not have to do any rounding on your longitude or latitude values, so use whichever one you want. Double is more precise, so let that be the most important factor in your decision.

您不必对经度或纬度值进行任何四舍五入,因此请使用您想要的任何一个。Double 更精确,所以让它成为您决定中最重要的因素。

How much precision do your long/lat values need?

您的 long/lat 值需要多少精度?

For pinpoint accuracy on a map, you might want to look at big decimal, but for vague values, float is good enough.

为了在地图上精确定位,您可能需要查看大十进制数,但对于模糊值,浮点数就足够了。

回答by Kai Sternad

You should start with the accuracyyou desire. Once you have determined that, you can choose a data type that is suitable.

你应该从你想要的准确性开始。确定后,您可以选择合适的数据类型。

If you decide that an accuracy of 5 decimal places (1.1132 m) is enough, you can easily go with float. The more accurate your calculation needs to be, the more you should lean towards using doubleand eventually BigDecimal.

如果您认为 5 个小数位 (1.1132 m) 的精度就足够了,您可以轻松地使用float. 您的计算需要越准确,您就越应该倾向于使用double和 最终BigDecimal

When comparing floating point numbers, you should incorporate the necessary precision as well.

在比较浮点数时,您还应该包含必要的精度。