MySQL floats get approximate values

MySQL fields of float type ain't passed to Elasticsearch with its exact values.

10.95 for instance transforms to 10.949999809265137.

Not a big difference, but becomes a problem while filtering against equal condition.

Any solution?

Well, floating-point values are by their very nature inexact. Exact comparisons are often not possible. Perhaps the original value isn't actually exactly 10.95 but you've been tricked into thinking so because of some rounding somewhere?

It is actually 10.95 in my MySQL db:

mysql> select `value` from `table`;
| value |
| 10.95 |
| 16.95 |

By in my particular case .round(2) solves the problem.

The 10.95 and 16.95 values you're seeing there have most likely been rounded by the mysql client program.

You are right!

mysql> select value*1000 from table;
| value*1000         |
| 10949.999809265137 |
| 16950.000762939453 |

Still, phpMyAdmin, MySQL Workbench and Sails-MySQL Adapter - each of these gives exact 10.95.

Just official MySQL JDBC driver doesn't?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.