Formatting Numbers with Commas in MariaDB

In MariaDB, we can use the FORMAT() function to format a number with commas.

More specifically, the function returns the number as a formatted string, with thousands separators and a decimal separator, and rounded to the given decimal position.

Example

It accepts a number as the first argument, followed by the decimal position:

SELECT FORMAT(123456.789, 2);

Result:

123,456.79

There’s no need to specify where the commas should go. The function knows where to put them.

Just to be sure, here’s another example with a larger number:

SELECT FORMAT(123456789.123456789, 6);

Result:

123,456,789.123457

Using a Comma for the Decimal Place

Commas are not always used for the group separator. In many locales, the period/full stop is used for the group separator, and the comma is used for the decimal place.

The FORMAT() function accepts a third argument that allows us to specify a locale to use for the formatting.

Here’s an example:

SELECT FORMAT(123456.789, 2, 'de_DE');

Result:

123.456,79

I set the locale to de_DE, which is for German. This time the comma is used as the decimal separator, and the full stop is used for the group separator.