FORMAT()

Description

Formats the number X to a format like ‘#,###,###.##’, rounded to D decimal places, and returns the result as a string.

Syntax

  1. > FORMAT(X,D[,locale])

Arguments

ArgumentsDescription
XRequired. X is the number need to format. If X is NULL, the function returns NULL.
DRequired. D is the number of decimal places to round.
If D is 0, the result has no decimal point or fractional part.
If D is NULL, the function returns NULL.
[,locale]Optional. The optional parameter enables a locale to be specified to be used for the result number’s decimal point, thousands separator, and grouping between separators. If the locale is NULL or not specified, the default locale is ‘en_US’. [,locale] supports locale parameters consistent with MySQL; see MySQL Server Locale Support.

Examples

  1. mysql> SELECT FORMAT(12332.123456, 4);
  2. +-------------------------+
  3. | format(12332.123456, 4) |
  4. +-------------------------+
  5. | 12,332.1235 |
  6. +-------------------------+
  7. 1 row in set (0.01 sec)
  8. mysql> SELECT FORMAT(12332.1,4);
  9. +--------------------+
  10. | format(12332.1, 4) |
  11. +--------------------+
  12. | 12,332.1000 |
  13. +--------------------+
  14. 1 row in set (0.00 sec)
  15. mysql> SELECT FORMAT(12332.2,0);
  16. +--------------------+
  17. | format(12332.2, 0) |
  18. +--------------------+
  19. | 12,332 |
  20. +--------------------+
  21. 1 row in set (0.00 sec)
  22. mysql> SELECT FORMAT(12332.2,2,'de_DE');
  23. +---------------------------+
  24. | format(12332.2, 2, de_DE) |
  25. +---------------------------+
  26. | 12.332,20 |
  27. +---------------------------+
  28. 1 row in set (0.00 sec)
  29. mysql> SELECT FORMAT(19999999.999999999,4);
  30. +-------------------------------+
  31. | format(19999999.999999999, 4) |
  32. +-------------------------------+
  33. | 20,000,000.0000 |
  34. +-------------------------------+
  35. 1 row in set (0.01 sec)
  36. mysql> SELECT FORMAT("-.12334.2","2", "en_US");
  37. +-----------------------------+
  38. | format(-.12334.2, 2, en_US) |
  39. +-----------------------------+
  40. | -0.12 |
  41. +-----------------------------+
  42. 1 row in set (0.00 sec)
  43. mysql> SELECT FORMAT("-.12334.2","2", "de_CH");
  44. +-----------------------------+
  45. | format(-.12334.2, 2, de_CH) |
  46. +-----------------------------+
  47. | -0.12 |
  48. +-----------------------------+
  49. 1 row in set (0.01 sec)