Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL storing Floats

I am trying to save a float number which is this long

13.00386644742523

Its basically Lat and Lng value. when i save it in the database its getting stored as

13.0039

like image 303
Harsha M V Avatar asked Jun 25 '12 08:06

Harsha M V


2 Answers

You should use the precision explicitly and the type should be DECIMAL NOT FLOAT as every digit after decimal point is significant and you cant let they allow to be changed because of rounding.

`lat`  DECIMAL(16,14)

Read on manual

like image 86
Shiplu Mokaddim Avatar answered Sep 21 '22 13:09

Shiplu Mokaddim


Based on experience I did this for a navigation database built from ARINC424 and eventually used a DECIMAL(18,12) for storing values in radians.

NOTE: Floats and doubles aren't as precise and may result in rounding errors

The point is that when using degrees or radians we know the range of the values - and the fractional part needs the most digits.

The best way is to use MySQL Spatial Extensions

like image 39
Richard Harrison Avatar answered Sep 24 '22 13:09

Richard Harrison