I'm trying to store float values in MySQL and my values seem to keep getting messed up. :(
I have my fields defined as float(10,7) and I round my values properly in PHP before inserting them:
$rndval = round($val,7)
INSERT INTO mytable (float) VALUES ($rndval)
But when I insert a value such as 47.5206797, it shows up as 47.5206795 in my table. Why is that?
If you need a value to be precise, store it as an exact data type such as DECIMAL(17,7), which would provide the same range as FLOAT(10,7). The only down side is that the DECIMAL will take up more disk space than the equivilant FLOAT, however this is trivial compared to correcting for floating point errors where precision is a concern.
http://dev.mysql.com/doc/refman/5.0/en/fixed-point-types.html
For more information on floating point number issues, the following may be worth a read
http://dev.mysql.com/doc/refman/5.0/en/problems-with-float.html
Related
My mysql version is 5.7.14
I have 1 table with two column
1). price_val_float with float data type
2). price_val_double with double data type
Table structure
CREATE TABLE test (
price_val_float FLOAT(6,2),
price_val_double DOUBLE(6,2)
);
Same value in both column
INSERT INTO test VALUES
(78.59, 78.59),
(78.60, 78.60),
(78.61, 78.61);
Now I set one variable as follow
SET #priceValue=78.6;
Now I want to get all record from test table where price_val_float >= #priceValue;
SELECT price_val_float FROM test WHERE price_val_float>= #priceValue;
above query return only 78.61
But if I run same query of price_val_double column
SELECT price_val_double FROM test WHERE price_val_double>= #priceValue;
This return
78.60
78.61
I am not getting why mysql return different result as only data type is different.
Does anyone knows about this ?
Here is Fiddle for testing
Thanks in advance.
This might sound strange to say but this is because decimal numbers are approximates values. This is an issue across all programming due to the nature of storing large numbers. Even the mysql documentation calls these "approximate" values:
https://dev.mysql.com/doc/refman/5.5/en/floating-point-types.html
For example: MySQL performs rounding when storing values, so if you insert 999.00009 into a FLOAT(7,4) column, the approximate result is 999.0001.
This is explained in the mysql documentation here:
https://dev.mysql.com/doc/refman/5.5/en/problems-with-float.html
Or as an additional case explained in Python here:
https://docs.python.org/2/tutorial/floatingpoint.html
The way to get around this is identify the precision you want and store the value as an integer.
Float is a single precision and Double is for double precision that why your getting the difference.
This is happening because the difference between the numbers shows up around the tenth decimal or so, depending on factors such as computer architecture or the compiler version or optimization level. For example, different CPUs may evaluate floating-point numbers differently.
You need to use DECIMAL data type for more accurate results. Also check this for more details
That is because Float point values are not stored as exact values. If you need exact value you can use Decimal data type. You can read about it here
I'm using phpmyadmin 4.0.10.7 on my server and I have a float column that sometimes doesn't work as itended: it is set as default: 0, but sometimes the value is entered as 0.00000000953674 when a new row is added.
Why is this happening?
Floating point numbers are not stored as exact values. See MySQL's writeup on the issue here.
Unless you have a specific need for floating point precision, I would recommend switching to the DECIMAL type, which does not have this problem (as of MySQL 5.0.3). If you must use floats, just remember that the values will not be exact, and direct comparisons will not always work (i.e. WHERE myVal = 5). Instead you should do ranged comparisons (i.e. WHERE myVal > 4.999 AND myVal < 5.001).
I have set my MySQL field table data type to the decimal because from what I have read, I would be able to store the price with commas/dots in the decimal data type fields... The problem is that whenever I store any data with the comma or dot, MySQL is rounding it automatically up or down. Eg. When I'm executing the following query:
UPDATE table SET field = 114.21 WHERE id = 1;
Then field is set, but the value is rounded to 114, instead of displaying the data I set in the query (114.21) - is there any solution for that? Or I should just use other data type?
AFAIK the dot is the standard notation for decimal values. Using Commas may trigger SQL parse errors or may go unnoticed if the syntactical context allows for a comma to be there.
How did you define the precision of the DECIMAL column?
If it is DECIMAL(10, 2) it will have a total of 10 numbers of which 2 are decimal values (with 2 decimal rounding meaning that 10.215 is saved as 10.22 and 10.214 becomes 10.21).
If it is DECIMAL(10) it will not have any decimal values and be rounded to an integer.
If you use FLOAT or DOUBLE PRECISION you don't have to specify the number of decimal values but it has its own flaws.
As Mihai mentioned you need to define the proper precision for the decimal type, e.g. DECIMAL(10,2) for two decimal places.
When inserting a decimal value mySQL will round.
From the docs:
For inserts into a DECIMAL or integer column, the target is an exact data type, so rounding uses “round half away from zero,” regardless of whether the value to be inserted is exact or approximate.
See http://dev.mysql.com/doc/refman/5.0/en/precision-math-rounding.html for details.
Well before I have also an issue regarding on what to use on my numbers with decimal points. But problem solved by using DOUBLE(10,2) as my DATATYPE, and it shows the exact number on the database when you save it. Hope it will help.
I'm trying to do a simple update query in MySQL but it doesn't work:
UPDATE dimensions
SET is_active = 1
WHERE eco_tax = 19.2;
eco_tax is a FLOAT type column and it seems that here is the problem, because when I try updating with an INT column it works.
So what cand I do to use a float in column in my where syntax in MySQL?
I always encounter issues when i want to do WHEREs in databases, and it is most likely a issue with floating point math. I know that doubles work the same way, but for some odd reason, it always works with using doubles. Therefore, my suggestion to you is to change the datatype to double instead.
Use double instead of float. It seems there has a bug of mySql. See the following link for more information.
http://bugs.mysql.com/bug.php?id=14268
The root of this problem is that some numbers cannot be represented exactly in floating point. You could try something like the following (unfortunately, I don't have access to a MySQL instance to hand to try this myself):
UPDATE dimensions
SET is_active = 1
WHERE ABS(eco_tax - 19.2) < 1E-08;
In other words, update if the difference between the two values is negligible.
try to alter the column to DECIMAL(x,y).
I had the same issue and I discovered it happens only on the rows when a floating value is used in the where clause; I changed the column type from FLOAT to DECIMAL(5,2) (of course choose your scale and precision) and... no more problems!.
I've got a column for storing float data, i.e.
1.1
11.60
4.23
Unfortunately, 11.60 gets stored as 11.6. I need it to have that extra zero. Do I have to change my datatype to varchar? What's the best way to handle this?
It sounds from the comments that you're storing a product code, so float isn't a good choice for a datatype, as you suggest. Indeed it's not a rendering issue, but we'd misconstrued it from your initial choice of float (thinking you indeed were storing something like money or true decimal).
Go with varchar, as you suspected, as it really is a string value.
Here's how you can do that:
create a new column of type varchar(100) or whatever length is suitable for you
copy the values into the new column from your float column
ALTER TABLE MyTable ADD MyNewColumn VARCHAR(100);
UPDATE MyTable
SET MyNewColumn = FORMAT(MyFloatColumn, 2);
This is a rendering issue, not a data issue. To "solve" it, apply mysql's FORMAT function to your value as you select it:
select FORMAT(my_float_column, 2)
from my_table;
The 2 is the number of decimal places. It will handle (almost) any number of digits to the left of the decimal place.