What would be effect of negative value of second argument, which specifies the number of decimal places, on the output of MySQL ROUND() function?

MySQLMySQLi Database

If we specify the negative value of the second argument then the digits before the decimal point would be deleted and rounded off. The number of digits to be deleted depends upon the value of the negative second argument. Following examples will demonstrate the change, depending upon the negative value of the second argument, in the output of ROUND() function.

mysql> Select ROUND(1789.456,-1);
+--------------------+
| ROUND(1789.456,-1) |
+--------------------+
|               1790 |
+--------------------+
1 row in set (0.00 sec)  

The query above returns 1790 because the first digit (which is to be deleted because of value -1) before the decimal point is 9 (which is > 5) hence the digit before this i.e. 8 would be rounded off to 9. 

mysql> Select ROUND(1789.456,-2);
+--------------------+
| ROUND(1789.456,-2) |
+--------------------+
|               1800 |
+--------------------+
1 row in set (0.00 sec)  

The query above returns 1800 because the second digit (which is to be deleted because of value -2) before the decimal point is 8 (which is > 5) hence the digit before this i.e. 7 would be rounded off to 8. 

mysql> Select ROUND(1789.456,-3);
+--------------------+
| ROUND(1789.456,-3) |
+--------------------+
|               2000 |
+--------------------+
1 row in set (0.00 sec)  

The query above returns 2000 because the third digit (which is to be deleted because of value -3) before the decimal point is 7 (which is > 5) hence the digit before this i.e. 1 would be rounded off to 2. 

mysql> Select ROUND(1789.456,-4);
+--------------------+
| ROUND(1789.456,-4) |
+--------------------+
|                  0 |
+--------------------+
1 row in set (0.00 sec)  

The query above returns 0 because a total number of digits before the decimal point is 4 and the value of the second argument is -4.

raja
Published on 20-Feb-2018 17:46:28
Advertisements