Format a Number as Currency in SQLite

SQLite has a PRINTF() function (and now a FORMAT() function) that allows us to format a number based on a format string. For example, we can use this to format the number to a given number of decimal places, plus add a currency symbol to it.

Example

Here’s an example to demonstrate:

SELECT PRINTF("$%.2f", 123.457);

Result:

$123.46

In this case, the result was rounded to two decimal places, due to our format string of $%.2f.

Here’s what happens if I pass a number that has less decimal places than specified:

SELECT PRINTF("$%.2f", 123);

Result:

$123.00

In this case, the result still shows two decimal places, even though they’re both zero.

See the SQLite documentation for more on the PRINTF() function.

The FORMAT() Function

SQLite 3.38.0 (released 22 Feb 2022) renamed the PRINTF() function to FORMAT(). The original PRINTF() name is retained as an alias for backwards compatibility.

Therefore, the first example above can be changed to this:

SELECT FORMAT("$%.2f", 123.457);

Result:

$123.46