Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL division precision

Tags:

mysql

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 |
+-----------------------------------------+
like image 396
neo Avatar asked Sep 13 '25 20:09

neo


1 Answers

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

like image 63
Sridhar DD Avatar answered Sep 15 '25 13:09

Sridhar DD