添加到数据库时 Laravel 浮动变量更改值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24024515/
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-09-14 09:36:10  来源:igfitidea点击:

Laravel float variable changing value when adding to database

phplaravellaravel-4eloquentlatitude-longitude

提问by

Theory

理论

I have quite a comprehensive coordinates list (Latitude and Longitude to 7 decimal places), which I want to continually add to. In order to stop data duplication, for each coordinate that's trying to be be inputted, I want to check the database to see if the latitude and longitude exist, on the same row.

我有一个相当全面的坐标列表(纬度和经度到小数点后 7 位),我想不断添加。为了停止数据重复,对于尝试输入的每个坐标,我想检查数据库以查看同一行上的纬度和经度是否存在。

Check to see if coordinates exist

检查坐标是否存在

 $coordinate = DB::table('coordinates')
            ->where('lat', '=', $geocode->getLatitude())
            ->where('lng', '=', $geocode->getLongitude())
            ->count();

Migration

移民

Schema::create('coordinates', function(Blueprint $table)
        {
            $table->increments('id');
            $table->float('lat', 10, 7);
            $table->float('lng', 10, 7);
            $table->timestamps();
        });

When the user's address is converted, I have noticed that when dd()the latitude variable, it comes out as:

当用户的地址被转换时,我注意到当dd()纬度变量时,它出来为:

float(53.7960957)from the code dd($geocode->getLatitude());

float(53.7960957)从代码 dd($geocode->getLatitude());

When I try to add it afterwards to the database, by removing the dd(), the actual decimal that's added into the database is 53.7960968- A completely different location when we're talking about coordinates!

当我之后尝试将它添加到数据库时,通过删除dd(),添加到数据库中的实际小数是53.7960968- 当我们谈论坐标时,这是一个完全不同的位置!

Why is the decimal changing from echoing on my screen, to adding to the database?

为什么小数点从屏幕上的回显变为添加到数据库中?

Do I need to convert it to a float before adding? How can I resolve this?

在添加之前是否需要将其转换为浮点数?我该如何解决这个问题?

采纳答案by Sam

Update:

更新:

I haven't used MySQL in a while, so I did some more research. It looks like the decimaltypehas the ability to be precise for up to 65 digits. Since you only need 7 digits passed the decimal and 3 before the decimal, you should have no problem creating a schema with decimal(10,7).

我有一段时间没有使用 MySQL,所以我做了一些更多的研究。看起来该decimal类型能够精确到 65 位数字。由于您只需要传递小数点后的 7 位数字和小数点前的 3 位数字,因此使用decimal(10,7).

SQL Fiddle

SQL小提琴



Assuming you are using MySQL, there is not much you can do. According to the documentation, floatand doubletypes are approximate representations:

假设您使用的是 MySQL,您无能为力。根据该文件float并且double类型是近似的表述:

The FLOAT and DOUBLE types represent approximate numeric data values. MySQL uses four bytes for single-precision values and eight bytes for double-precision values.

FLOAT 和 DOUBLE 类型表示近似数值数据值。MySQL 对单精度值使用四个字节,对双精度值使用八个字节。

Also check out this SQL Fiddle, you'll see that MySQL 5.5.32 will represent a float of 53.7960957as 53.796100616455and a double of 53.7960957as 53.7960957. You may want to update your schema to use doubles instead, for slightly more precision.

也看看这个SQL小提琴,你会看到MySQL的32年5月5日将是一个浮动的53.7960957作为53.796100616455和双的53.7960957作为53.7960957。您可能希望更新您的架构以使用双精度数,以获得更高的精度。

You can also specify the precision of a float with nonstandard syntax (so it is not recommended, for portability to other SQL standards) by using float(m,d)where mis the total digits and dis the number of digits after the decimal. For instance, if you want to allow latitude/longitude values (-180 to 180) with up to 7 digits of precision after the decimal..you should be able to create your table with float(10,7). But, as you'll see, this still wasn't as precise as double was.

您还可以通过使用float(m,d)wherem是总位数和d小数点后的位数来指定具有非标准语法的浮点数的精度(因此不建议使用,以便可移植到其他 SQL 标准)。例如,如果您希望允许纬度/经度值(-180 到 180)在小数点后具有多达 7 位的精度……您应该能够使用float(10,7). 但是,正如您将看到的,这仍然不像 double 那样精确。

回答by EselDompteur

as the forewriter say's ... use DOUBLE instad of FLOAT ...

正如前任所说的......使用DOUBLE instad of FLOAT ...

you could also use 1E6 notation so it would be an Integer to save is MySQL.

你也可以使用 1E6 表示法,所以它会是一个整数来保存是 MySQL。

53.7960957 * 1E6 = 537960957
537960957 / 1E6 = 53.7960957

this is the way I am using to save geocoordinates

这是我用来保存地理坐标的方式