Version sans cache

Mise en cache désactivé. Réglage défaut pour cette page : actif (code DEF204)
Si l'affichage est trop lent, vous pouvez désactiver le mode utilisateur pour visualiser la version en cache.

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:

  1. mysql> SELECT ROUND(2.5), ROUND(25E-1);
  2. +------------+--------------+
  3. | ROUND(2.5) | ROUND(25E-1) |
  4. +------------+--------------+
  5. | 3          |            2 |
  6. +------------+--------------+

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:

  1. mysql> CREATE TABLE t (d DECIMAL(10,0));
  2. Query OK, 0 rows affected (0.00 sec)
  3.  
  4. mysql> INSERT INTO t VALUES(2.5),(2.5E0);
  5. Query OK, 2 rows affected, 2 warnings (0.00 sec)
  6. Records: 2  Duplicates: 0  Warnings: 2
  7.  
  8. mysql> SHOW WARNINGS;
  9. +-------+------+----------------------------------------+
  10. | Level | Code | Message                                |
  11. +-------+------+----------------------------------------+
  12. | Note  | 1265 | Data truncated for column 'd' at row 1 |
  13. | Note  | 1265 | Data truncated for column 'd' at row 2 |
  14. +-------+------+----------------------------------------+
  15. 2 rows in set (0.00 sec)
  16.  
  17. mysql> SELECT d FROM t;
  18. +------+
  19. | d    |
  20. +------+
  21. |    3 |
  22. |    3 |
  23. +------+
  24. 2 rows in set (0.00 sec)

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”).


Rechercher dans le manuel MySQL

Traduction non disponible

Le manuel MySQL n'est pas encore traduit en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.

Document créé le 26/06/2006, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/mysql-rf-precision-math-rounding.html

L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.

Références

  1. Consulter le document html Langue du document :en Manuel MySQL : https://dev.mysql.com/

Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.

Table des matières Haut