I created a table and inserted a row of data using the statement below
CREATE TABLE float_example(
id INT AUTO_INCREMENT PRIMARY KEY,
float_col FLOAT
);
INSERT INTO float_example (id,float_col) VALUES (1,123.456789);
When using the query statement below for verification, it can be seen that the actual precision has reached at least four decimal places, but only two are displayed
SELECT ROUND(float_col,6),float_col from float_example where id=1;
How should I set it up to display the maximum precision that IEEE floating-point numbers can represent in MySQL?
flan ttt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
5
Since you do not provide any precision, your FLOAT value is not restricted
According to documentation:
For maximum portability, code requiring storage of approximate numeric data values should use FLOAT or DOUBLE PRECISION with no specification of precision or number of digits.
So it means that you can store virtually any precision.
What you see with float_col
being 123.457
is just MySql presentation of float. To get values in needed precision, use rounding (as in your first select operation)
Check this DB Fiddle
As you can see, there is even more values in floating point, than you inserted. That’s why you can’t accurratly compare float values.
To properly compare floats, use methods listed here
1