21.4. Rounding Behavior

MySQL 5.0

21.4. Rounding Behavior

This section discusses precision math rounding for the function and for inserts into columns with exact-value types ( and integer).

The function rounds differently depending on whether its argument is exact or approximate:

  • For exact-value numbers, uses the “round half up” rule: A value with a fractional part of .5 or greater is rounded up to the next integer if positive or down to the next integer if negative. (In other words, it is rounded away from zero.) A value with a fractional part less than .5 is rounded down to the next integer if positive or up to the next integer if negative.

  • For approximate-value numbers, the result depends on the C library. On many systems, this means that uses the “round to nearest even” rule: A value with any fractional part is rounded to the nearest even integer.

The following example shows how rounding differs for exact and approximate values:

mysql> 
+------------+--------------+
| ROUND(2.5) | ROUND(25E-1) |
+------------+--------------+
| 3          |            2 |
+------------+--------------+

For inserts into a or integer column, the target is an exact data type, so rounding uses “round half up,” regardless of whether the value to be inserted is exact or approximate:

mysql> 
Query OK, 0 rows affected (0.00 sec)

mysql> 
Query OK, 2 rows affected, 2 warnings (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 2

mysql> 
+------+
| d    |
+------+
| 3    |
| 3    |
+------+