round

description

Syntax

T round(T x[, d]) Rounds the argument x to d decimal places. d defaults to 0 if not specified. If d is negative, the left d digits of the decimal point are 0. If x or d is null, null is returned. 2.5 will round up to 3. If you want to round down to 2, please use the round_bankers function.

If d is a column, and x has Decimal type, scale of result Decimal will always be same with input Decimal.

ROUND - 图1tip

Another alias for this function is dround.

example

  1. mysql> select round(2.4);
  2. +------------+
  3. | round(2.4) |
  4. +------------+
  5. | 2 |
  6. +------------+
  7. mysql> select round(2.5);
  8. +------------+
  9. | round(2.5) |
  10. +------------+
  11. | 3 |
  12. +------------+
  13. mysql> select round(-3.4);
  14. +-------------+
  15. | round(-3.4) |
  16. +-------------+
  17. | -3 |
  18. +-------------+
  19. mysql> select round(-3.5);
  20. +-------------+
  21. | round(-3.5) |
  22. +-------------+
  23. | -4 |
  24. +-------------+
  25. mysql> select round(1667.2725, 2);
  26. +---------------------+
  27. | round(1667.2725, 2) |
  28. +---------------------+
  29. | 1667.27 |
  30. +---------------------+
  31. mysql> select round(1667.2725, -2);
  32. +----------------------+
  33. | round(1667.2725, -2) |
  34. +----------------------+
  35. | 1700 |
  36. +----------------------+
  37. mysql> SELECT number
  38. -> , round(number * 2.5, number - 1) AS r_decimal_column
  39. -> , round(number * 2.5, 0) AS r_decimal_literal
  40. -> , round(cast(number * 2.5 AS DOUBLE), number - 1) AS r_double_column
  41. -> , round(cast(number * 2.5 AS DOUBLE), 0) AS r_double_literal
  42. -> FROM test_enhanced_round
  43. -> WHERE rid = 1;
  44. +--------+------------------+-------------------+-----------------+------------------+
  45. | number | r_decimal_column | r_decimal_literal | r_double_column | r_double_literal |
  46. +--------+------------------+-------------------+-----------------+------------------+
  47. | 1 | 3.0 | 3 | 3 | 3 |
  48. +--------+------------------+-------------------+-----------------+------------------+

keywords

  1. ROUND, DROUND