我有一个存储经度和纬度坐标的表(谷歌 map ),我将列定义为 float ,但是当我尝试插入值-61.45859899999999和10.28289时,它们会四舍五入为-61.46和10.30。我如何修改列以按原样保存数据。

我正在使用 mysql toad 。下面是该表的代码:

CREATE TABLE `tblGeoCodes` (
  `recNo` int(11) NOT NULL AUTO_INCREMENT,
  `longLocation` float(30,2) DEFAULT NULL,
  `latLocation` float(30,2) DEFAULT NULL

最佳答案

您的实现存在两个问题。

将值都四舍五入为2位精度的原因是您将小数位明确定义为2。

另外,FLOAT是MySQL中不精确的数据类型。

要解决这两个问题,应使用具有适当精度和小数位数的DECIMAL数据类型。

例如,如下所示:

CREATE TABLE `tblGeoCodes` (
  `recNo` int(11) NOT NULL AUTO_INCREMENT primary key,
  `longLocation` decimal(18,14) DEFAULT NULL,
  `latLocation` decimal(18,14) DEFAULT NULL
);

例子:
mysql> CREATE TABLE `tblGeoCodes` (
    ->   `recNo` int(11) NOT NULL AUTO_INCREMENT primary key,
    ->   `longLocation` decimal(18,14) DEFAULT NULL,
    ->   `latLocation` decimal(18,14) DEFAULT NULL
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql>
mysql> insert into tblGeoCodes (longLocation,latLocation) values(-61.45859899999999 , 10.28289);
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> select * from tblGeoCodes;
+-------+--------------------+-------------------+
| recNo | longLocation       | latLocation       |
+-------+--------------------+-------------------+
|     1 | -61.45859899999999 | 10.28289000000000 |
+-------+--------------------+-------------------+
1 row in set (0.00 sec)

10-01 06:33
查看更多