Lesson

MySQL FORMAT() Function

10 min reading
Free Course

MySQL FORMAT() Function

The FORMAT() function rounds a number to a chosen decimal precision, inserts grouping separators, and returns the formatted result as a string. Use it for final presentation in reports rather than intermediate calculations or database storage.

Syntax

FORMAT(X, D [, locale])
  • X: The numeric value to format.
  • D: Number of decimal places (0 omits the decimal point and fractional digits).
  • locale: Optional locale tag like 'en_US' (default), 'de_DE', or 'fr_FR' for regional separators.
  • Return value: Formatted text string, or NULL if X or D is NULL.

Because FORMAT() outputs text, MySQL adds commas (1,250.50) or locale-specific dots (1.250,50) that numeric columns cannot store natively. Regional locales also let international finance teams match local invoice standards directly in SQL views.

Practical Example

Suppose finance wants readable invoice totals from demo_orders, showing both US and German number conventions while keeping rows sorted by actual order value.

SELECT
    order_id,
    order_code,
    FORMAT(total_amount, 2) AS us_total,
    FORMAT(total_amount, 2, 'de_DE') AS de_total
FROM demo_orders
ORDER BY total_amount DESC;
order_id order_code us_total de_total
88 ORD-410 12,450.75 12.450,75
85 ORD-407 980.50 980,50
82 ORD-404 45.00 45,00

Notice the ORDER BY clause sorts on the raw total_amount column, not the formatted alias. Formatting happens only in the SELECT projection so numeric order stays accurate.

Common Mistakes & NULL Behavior

  • Sorting by the formatted string: Because FORMAT() returns text, '12,450.75' sorts before '980.50' alphabetically ('1' < '9'). Always sort by the original numeric column instead.
  • Doing math on formatted strings: Adding '1,250.00' + 10 stops parsing at the comma, treating '1,250.00' as 1 and returning 11. Use ROUND() when downstream queries need a numeric type.
  • Negative D values: Passing a negative decimal count is treated as 0, omitting decimals entirely.
  • NULL inputs: Passing NULL for either X or D returns NULL. Invalid locale names fall back to 'en_US' with a warning.

Frequently Asked Questions

What is the difference between FORMAT() and ROUND()?

ROUND() returns a numeric type without thousands commas. FORMAT() rounds the value and returns a display string with grouping separators.

How do I remove decimal places with FORMAT()?

Pass 0 as the second argument: FORMAT(1250.8, 0) rounds to the nearest integer and returns '1,251'.

Does FORMAT() always round half up?

Exact numeric types (DECIMAL) round half away from zero (1.255 becomes '1.26'), whereas FLOAT or DOUBLE values follow binary floating-point rounding rules.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
MySQL Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum