MariaDB FORMAT(): Group Thousands and Set Decimal Places

MariaDB FORMAT(number, decimal_places[, locale]) returns text with digit grouping and a rounded decimal part. FORMAT(1234567.89, 2) gives 1,234,567.89. Sort and calculate on numbers, then use FORMAT for display. MariaDB Docs · FORMAT()

Choose the number of decimal places

The first two queries run in a SQL client without a table. The second argument controls the number of decimal places. Zero omits the fractional part, and two adds .00 to an integer. The function rounds, so 999.995 at two decimal places displays as 1,000.00.

SELECT FORMAT(123456789, 0) AS grouped,
       FORMAT(123456789, 2) AS padded,
       FORMAT(1234567.891, 2) AS rounded,
       FORMAT(-1234.567, 2) AS negative_value,
       FORMAT('123456789', 0) AS numeric_string,
       FORMAT(999.995, 2) AS carry;
Result column Displayed value
grouped 123,456,789
padded 123,456,789.00
rounded 1,234,567.89
negative_value -1,234.57
numeric_string 123,456,789
carry 1,000.00

Use locale separators and preserve NULL

SELECT FORMAT(1234567.89, 2, 'en_US') AS en_us,
       FORMAT(1234567.89, 2, 'de_DE') AS de_de,
       FORMAT(NULL, 2) AS missing_value;

The optional third argument is a locale, which selects regional formatting rules. en_US gives 1,234,567.89 and de_DE gives 1.234.567,89. Missing NULL amounts remain NULL. Decide separately whether a display should show zero for a missing amount. MariaDB Docs · Server Locale

Sort and sum the original numeric values

Run this block once in a practice database named learning, with permission to create, insert, and select. Use a separate practice database if the table already exists. Keep amount as DECIMAL. Compare the rounded numeric result rounded_number with the output text displayed. MariaDB Docs · DECIMAL MariaDB Docs · ROUND()

USE learning;
CREATE TABLE format_values_demo (
    amount DECIMAL(12,3) NULL
);
INSERT INTO format_values_demo (amount) VALUES
    (2), (10), (1000), (1234567.891), (NULL);
SELECT amount,
       FORMAT(amount, 2) AS displayed,
       ROUND(amount, 2) AS rounded_number
FROM format_values_demo
ORDER BY amount;
SELECT FORMAT(amount, 2) AS displayed
FROM format_values_demo
WHERE amount IS NOT NULL
ORDER BY displayed;

SELECT FORMAT(amount, 2) AS displayed
FROM format_values_demo
WHERE amount IS NOT NULL
ORDER BY amount;

SELECT SUM(amount) AS raw_total,
       FORMAT(SUM(amount), 2) AS displayed_total
FROM format_values_demo;

The first sort compares text and gives 1,000.00 → 1,234,567.89 → 10.00 → 2.00. The second compares the original numbers and gives 2.00 → 10.00 → 1,000.00 → 1,234,567.89. The last query sums the raw values and displays 1,235,579.89. Use the numeric column for sorting and aggregation.

Handle input that already contains separators

SELECT FORMAT('1,234.56', 2) AS wrong_input;
SHOW WARNINGS;

The input below is already display text with a comma. The query produces 1.00 with a numeric conversion warning in the example. Run SHOW WARNINGS immediately in the same connection. A formatted result does not tell you whether the full input was understood.

Validate user input against the expected regional format and numeric range, then parse and bind a numeric value. Removing all commas can change the meaning in locales where a comma marks the decimal part. If the application already localizes numbers, keep the SQL output numeric and apply formatting once in the presentation layer.