mySQL float column set as default 0 sometimes contains wierd value - mysql

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).

Related

How to insert/update decimal into mysql via PHPmyadmin?

I want to insert/update decimal number to mysql. But everytime I did. It return the round number or truncate dot number. I tried change the datatype of lv_pay and lv_dis either to decimal and double but still the result.
MySQL
update settings_price_pay set lv_pay='3.2',lv_dis='0' where pset='1' and cate='161a5954c2e7713417906c523204a2be' and ltype='p_rhi'
PHPMyadmin
First:
The data type of those numeric fields should be DECIMAL(12,2) or something similar, declaring that you use a picture of S#########9.99. Sign, ten digits, point, two digits.
Second:
Don't put your numbers in 'quotes'. If you do, MySQL first coerces them to IEEE 64-bit numbers, then to whatever datatype you have for your columns. Say this:
set lv_pay=3.2, lv_dis=0
Notice that MySQL ignores the numbers in parentheses in DOUBLE(11,2) and simply uses a 64-bit IEEE floating point number. (It honors those numbers when you declare a DECIMAL(12,2) data type.)
Got an answer, just for novice like me. Change the 'length/value' of the row (in my case) from (11,0) into (11,2). Found it accidentally.

Mysql where clause comparison give different result for double and float data type for same value

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

Float value stored in MySQL keeps getting messed up

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

Update with float type in MySQL

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!.

MySQL - avoid truncating trailing zeros in float datatype

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.