php - Laravel float variable changing value when adding to database -
theory
i have quite comprehensive coordinates list (latitude , longitude 7 decimal places), want continually add to. in order stop data duplication, each coordinate that's trying be inputted, want check database see if latitude , longitude exist, on same row.
check 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 user's address converted, have noticed when dd()
latitude variable, comes out as:
float(53.7960957)
code dd($geocode->getlatitude());
when try add afterwards database, removing dd()
, actual decimal that's added database 53.7960968
- different location when we're talking coordinates!
why decimal changing echoing on screen, adding database?
do need convert float before adding? how can resolve this?
update:
i haven't used mysql in while, did more research. looks decimal
type has ability precise 65 digits. since need 7 digits passed decimal , 3 before decimal, should have no problem creating schema decimal(10,7)
.
assuming using mysql, there not can do. according the documentation, float
, double
types approximate representations:
the float , double types represent approximate numeric data values. mysql uses 4 bytes single-precision values , 8 bytes double-precision values.
also check out sql fiddle, you'll see mysql 5.5.32 represent float of 53.7960957
53.796100616455
, double of 53.7960957
53.7960957
. may want update schema use doubles instead, more precision.
you can specify precision of float nonstandard syntax (so not recommended, portability other sql standards) using float(m,d)
m
total digits , d
number of digits after decimal. instance, if want allow latitude/longitude values (-180 180) 7 digits of precision after decimal..you should able create table float(10,7)
. but, as you'll see, still wasn't precise double was.
Comments
Post a Comment