What's the best way to store decimal degrees in MySQL? - mysql

I have latitude and longitude columns in my table.
I'm currently storing latitude with float(16,14) and longitude with float(17,14). Is that the best way to store them? The values I'm inserting are from JS navigator.geolocation, and they don't tend to have more than 14 digits after the decimal place.
Should I be using decimal instead?

You should use decimal! Read on this: Problems with Floating-Point Values

The DECIMAL type stores exact numeric data values (if you use MySQL5.0.3 or above), the FLOAT type represents approximate numeric data values.
Since as far as I know latitude and longitude values are usually exact numeric data values for most use cases, I'd go for a DECIMAL.

Related

how to find float values in mysql database with rails

Let's say I have a rails model called Position and the two columns latitude and longitude (both defined as float). I have populated the model with some position values. Now I'm trying to find positions based on it's latitude:
positions = Position.where('positions.latitude = ?', 50.0)
returns nil.
Even if I try this:
lat = Position.first.latitude
positions = Position.where('positions.latitude = ?', lat)
the result is nil. My database is mysql for production. The code above words in development (sqlite). My assumption is it has something to do with how the datatypes are handled but I dont get it. Anyone some ideas?
Don't use FLOAT or DOUBLE when you need exact numeric values.
Read https://dev.mysql.com/doc/refman/8.0/en/problems-with-float.html
Instead, use DECIMAL or NUMERIC (they're synonyms).
See How accurately should I store latitude and longitude? for some guidance on the specific DECIMAL precision to use for latitude and longitude.
This is maybe due to how floating point numbers are represented. It's not possible to compare them by using a normal equality comparison. Instead of checking for the exact match it's better to check if the number is in a range or round both numbers to some precision before comparing them.
Regarding the particular example with geographical coordinates (I assume this because latitude is used), I would recommend using decimal column type instead of float.
In case you would like to stick with floatcolumn, I can suggest checking if the value is in a range.

How do I change precision of float values in mysql?

Hello in my database I have a column with numbers such as 6.251543423 I want to make them like 6.25 without rounding them.
I've tried update examresults set point = substring(point,0,5) but it returned all values as zero
If you don't want to round you must truncate:
TRUNCATE(6.251543423,2)
When you CAST as DECIMAL(n,2) or FLOAT(n,2) the result will be rounded.
Convert the floating point values to DECIMAL datatype, with appropriate number of digits after the decimal point. In this example, two digits after the decimal point.
SELECT CONVERT(float_col,DECIMAL(65,2)) AS dec_col
FROM ...
The floating point types FLOAT and DOUBLE are approximate decimal values. They are stored in standard IEEE floating point representation.
If you convert to decimal, and then store that back in a floating point column, it will be converted back into floating point representation.
You would need to do the conversion to DECIMAL when you pull the value back out again.
If you want to store an exact decimal representation, you would need to store the value in a column defined as DECIMAL (or NUMERIC) datatype, not a FLOAT or DOUBLE.
Also...
MySQL permits a nonstandard syntax: FLOAT(M,D) or REAL(M,D) or DOUBLE PRECISION(M,D). Here, “(M,D)” means than values can be stored with up to M digits in total, of which D digits may be after the decimal point. For example,
(that's excerpted from MySQL Reference Manual: https://dev.mysql.com/doc/refman/5.5/en/floating-point-types.html )
(NOTE: I've never exercised/tested that behavior of DOUBLE(18,2). When we need exact decimals, we use DECIMAL. And when we need floating point, we use plain old DOUBLE.)

How to properly define the [float] data type in mysql

I defined (through PHPMYADMIN) a field with the float data type.
When I try to insert a number as 78.556677099932222377 it will translate it to 79.
How do I make it to save the correct number, at least 7 places after the decimal dot?
Open phpMyAdmin with structure option and define the float as:
FLOAT(23,19)
clear in this picture:
How do you define the float in phpMyAdmin
FLOAT(23,19)
when u declare the field you could use that above.
Better to go with Cygnusx1 and change to decimal
see MySQL numeric types
and also Problems with Float
DECIMAL is what you looking for.
ref:
SQL Decimal is used to specify the values in decimal datatype. The Decimal datatype can be of two types : Decimal (p) refers to floating point and Decimal fixed point (p,s). The DECIMAL data type can store decimal floating-point numbers up to a maximum of 32 significant digits and Decimal fixed point can store upto 16 significant digits.

MySQL Decimal Clarification

When using decimal in MySQL, does a value of (5,3) make it so the max number is 99.999? If not how do I specify that? My number is for currency so I need to use decimal, but it should not go over 99 and it must have 3 decimal places.
decimal (5,3) is the standard SQL way to define a field with a range of -99.999 .. 99.999.
Note that with MySQL, if you attempt to insert a value that is out of range, MySQL will automatically truncate the value to the nearest boundary value (in this case, either 99.999 or -99.999) without throwing an error.
Added
In this case, there will be a maximum of 5 digits of data stored, with exactly 3 digits to the right of the decimal point. If you insert a number like 1.23 it will be stored as 1.230; likewise 1 will be stored as 1.000. If you insert 12.3456 it will be rounded up to 12.346; if you insert 12.0001 it will be rounded down to 12.000.
When the column is returned to your program, the exact representation will depend upon how your database API maps a decimal data type. If it maps to an integer, you'll lose the decimal fraction; if it maps to a floating point number (float, double), you may lose precision; if it maps to a Decimal (C#/Java) then you'll get an exact representation, as long as your value is within the bounds of the datatype.
The method you specified should work. I don't see any problems with it.

MySQL FLOAT & decimals

Datatype of field in the DB is FLOAT and the value is 18.7. I'd like to store and display this on page as 18.70. Whenever I enter the extra 0 it still only stores it as 18.7
How can I store the extra 0 ? I can change the data type of the field.
In a FLOAT column, what MySQL stores for 18.7, is actually:
01000001 10010101 10011001 10011010
which, being retrieved from the DB and converted back into your display format, is 18.7.
In reality, the stored value is a binary fraction represented by the decimal number 18.70000076293945 which you can see by issuing this query:
CREATE TABLE t_f (value FLOAT);
INSERT
INTO t_f
VALUES (18.7);
SELECT CAST(value AS DECIMAL(30, 16))
FROM t_f;
IEEE-754 representation of number stores them as binary fractions, so a value like 0.1 can only be represented with continued fraction and hence be not exact.
DECIMAL, on the other hand, stores decimal digits, packing 9 digits into 4 bytes.
Floating point types do not store the number of insignificant zeros on the left side of a number before decimal digit or on the right side of the number after the decimal digit. You'll need to use a string-based type (or store the precision in a separate field) if you want to store the exact numeric string entered by the user and be able to distinguish 12.7 from 12.70. You can, however, round things that you display by two digits in your application.
if two decimal points needed use:
decimal(n,2); where n>=2
the decimal data type will persist the decimal points formatting and gives more accurate results than float and double data types.
Are you attempting to store a currency as a float? If so, please use a decimal with more decimal digits than 2.
You really want fixed-point arithmetic on currencies.
This is just very broad rule of thumb and my own observation, but in regular business logic as serialized in a database, you almost never want floating point. I know there are lots of exceptions, but I'm suspicious whenever I see a float typed column in a table because of this. I'd be interested in what others have found.