neo
neo

Reputation: 115

MySQL division precision

mysql> SELECT 100 / 35600.00 * 35600.00;

+---------------------------+
| 100 / 35600.00 * 35600.00 |
+---------------------------+
|                 99.999973 |
+---------------------------+

mysql> SELECT TRUNCATE(100 / 35600.00, 30) * 35600.00;

+-----------------------------------------+
| TRUNCATE(100 / 35600.00, 30) * 35600.00 |
+-----------------------------------------+
|       99.999972800000000000000000000000 |
+-----------------------------------------+

Upvotes: 4

Views: 2629

Answers (1)

Sridhar DD
Sridhar DD

Reputation: 1980

You should use TRUNCATE() in MySQL for your purpose:

SELECT TRUNCATE(100 / 35600.00 * 35600.00, 2);

It will provide you result as 99.99

There's a problem with using TRUNCATE() - it will not round off. So I suggest you to use ROUND() function instead:

SELECT ROUND(100 / 35600.00 * 35600.00, 2);

Result: 100.00

Upvotes: 10

Related Questions