I store the values ββin a column defined as a float. When I read these values ββfrom the database, they sometimes differ a lot more from the original values ββthan I would expect (I know that the float does not preserve the exact values, please take a look at the example to understand what I mean).
Here is my test case:
drop table if exists float_test; create table float_test (value1 float(8, 3), value2 float); insert into float_test values (11743.85, 11743.85); select mt.*, round(mt.value1, 6), round(mt.value2, 6) from float_test mt;
Selection Results:
mysql> select mt.*, round(mt.value1, 6), round(mt.value2, 6) from float_test mt; +-----------+---------+---------------------+---------------------+ | value1 | value2 | round(mt.value1, 6) | round(mt.value2, 6) | +-----------+---------+---------------------+---------------------+ | 11743.850 | 11743.8 | 11743.849609 | 11743.849609 | +-----------+---------+---------------------+---------------------+ 1 row in set (0.01 sec)
As you can see, choosing value2 leads to 11743.8, while choosing round (value2, 6) leads to a value that is much closer to the one I originally put in. Also if you
mysql> select * from float_test mt where value1 = value2; +-----------+---------+ | value1 | value2 | +-----------+---------+ | 11743.850 | 11743.8 | +-----------+---------+ 1 row in set (0.00 sec)
you can see that the values ββstored in values ββ1 and 2 are actually equal. So, I think it is just a question of how the results are displayed. Looking through the MySQL documentation, I could not find any rules that say float values ββare automatically rounded for display.
So now I have towing questions:
1. What rules do mysql use to display float values?
2. Is there a way (for example, some configuration parameters) that I can get a value that is not rounded to the first decimal place without changing the definition of my table and without changing my select statement?
I tested this on mysql 4.0, 5.0 and 5.1.
Thanks,
Stephen