纬度/经度8位小数应使用哪种MySQL数据类型? [英] What MySQL data type should be used for Latitude/Longitude with 8 decimal places?
问题描述
我正在使用地图数据,经纬度
扩展到8位小数位。例如:
纬度40.71727401
经度-74.00898606
我在 Google文档
使用:
lat FLOAT(10,6)NOT NULL ,
lng FLOAT(10,6)NOT NULL
但是,到6.
我应该使用 FLOAT(10,8)
还是有另一种方法来考虑存储这个数据,所以它是精确的。它将用于地图计算。谢谢!
DECIMAL是用于精确算术的MySQL数据类型。与FLOAT不同的是,它的精度是固定的任何数量的大小,所以通过使用它而不是FLOAT你可能会避免精确度的错误,当做一些计算。如果您只是在没有计算的情况下存储和检索数字,那么在实践中FLOAT将是安全的,尽管使用DECIMAL没有什么坏处。通过计算,FLOAT仍然大部分没有问题,但要绝对确定8d.p.精度,你应该使用DECIMAL。
纬度范围从-90到+90(度),所以十进制(10,8)是好的,但经度范围从 - 180到+180(度),所以你需要DECIMAL(11,8)。第一个数字是存储的总位数,第二个是小数点后面的数字。
总之: lat DECIMAL(10 ,8)NOT NULL,lng DECIMAL(11,8)NOT NULL
这个解释了MySQL如何与浮点数据类型一起工作。
I'm working with map data, and the Latitude/Longitude
extends to 8 decimal places. For example:
Latitude 40.71727401
Longitude -74.00898606
I saw in the Google document which uses:
lat FLOAT( 10, 6 ) NOT NULL,
lng FLOAT( 10, 6 ) NOT NULL
however, their decimal places only go to 6.
Should I use FLOAT(10, 8)
or is there another method to consider for storing this data so it's precise. It will be used with map calculations. Thanks!
DECIMAL is the MySQL data-type for exact arithmetic. Unlike FLOAT its precision is fixed for any size of number, so by using it instead of FLOAT you might avoid precision errors when doing some calculations. If you were just storing and retrieving the numbers without calculation then in practice FLOAT would be safe, although there's no harm in using DECIMAL. With calculations FLOAT is still mostly ok, but to be absolutely sure of 8d.p. precision you should use DECIMAL.
Latitudes range from -90 to +90 (degrees), so DECIMAL(10, 8) is ok for that, but longitudes range from -180 to +180 (degrees) so you need DECIMAL(11, 8). The first number is the total number of digits stored, and the second is the number after the decimal point.
In short: lat DECIMAL(10, 8) NOT NULL, lng DECIMAL(11, 8) NOT NULL
This explains how MySQL works with floating-point data-types.
这篇关于纬度/经度8位小数应使用哪种MySQL数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!