Rechercher dans le manuel MySQL
12.25.4 Rounding Behavior
This section discusses precision math rounding for the
ROUND()
function and for inserts
into columns with exact-value types
(DECIMAL
and integer).
The ROUND()
function rounds
differently depending on whether its argument is exact or
approximate:
For exact-value numbers,
ROUND()
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. (In other words, it is rounded toward zero.)For approximate-value numbers, the result depends on the C library. On many systems, this means that
ROUND()
uses the “round to nearest even” rule: A value with a fractional part exactly half way between two integers is rounded to the nearest even integer.
The following example shows how rounding differs for exact and approximate values:
- +------------+--------------+
- +------------+--------------+
- | 3 | 2 |
- +------------+--------------+
For inserts into a DECIMAL
or
integer column, the target is an exact data type, so rounding uses
“round half away from zero,” regardless of whether
the value to be inserted is exact or approximate:
- Query OK, 0 rows affected (0.00 sec)
- +-------+------+----------------------------------------+
- | Level | Code | Message |
- +-------+------+----------------------------------------+
- +-------+------+----------------------------------------+
- +------+
- | d |
- +------+
- | 3 |
- | 3 |
- +------+
The SHOW WARNINGS
statement
displays the notes that are generated by truncation due to
rounding of the fractional part. Such truncation is not an error,
even in strict SQL mode (see
Section 12.25.3, “Expression Handling”).
Document created the 26/06/2006, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/mysql-rf-precision-math-rounding.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.