Laravel floating-point variable value when added to the database

Theory

I have a fairly complete list of coordinates (latitude and longitude up to 7 decimal places), to which I want to constantly add. To stop data duplication, for each coordinate that is trying to be entered, I want to check the database to see if latitude and longitude exist on the same line.

Check 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 address is translated, I noticed that when the dd()latitude variable, it is output as:

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

When I try to add it subsequently to the database by deleting dd(), the actual decimal number added to the database 53.7960968is a completely different place when we talk about coordinates!

, ?

float ? ?

+4
2

Update:

MySQL , . , decimal type 65 . 7 , 3 , decimal(10,7).

SQL Fiddle


, MySQL, . , float double :

FLOAT DOUBLE . MySQL .

SQL Fiddle, , MySQL 5.5.32 53.7960957 53.796100616455 double 53.7960957 53.7960957. , , , .

( SQL) float(m,d), m - , d - . , / ( -180 180) 7 . float(10,7). , , , .

+4

... DOUBLE instad FLOAT...

1E6, Integer MySQL.

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

,

+3

Source: https://habr.com/ru/post/1543129/


All Articles