MySQL FORMAT() Function
The FORMAT()
function in MySQL is used to format a number into a user-friendly string representation, similar to how you might want to display currency or other numerical values.
FORMAT() Function: Definition and Usage
This function takes a number and formats it with commas as thousands separators and a specified number of decimal places. It rounds the number to the specified decimal places before formatting it as a string.
Syntax
Syntax
FORMAT(number, decimal_places)
Parameter Values
Parameter | Description |
---|---|
number |
The number you want to format. This is required. |
decimal_places |
The number of decimal places to round the number to. This is required. If set to 0, no decimal places are included in the output. |
Examples
Formatting with Two Decimal Places
This example formats a number to have two decimal places, rounding if necessary.
Syntax
SELECT FORMAT(250500.5634, 2);
Output
250,500.56
Formatting with Zero Decimal Places
This example formats a number to have no decimal places (it's rounded to the nearest whole number).
Syntax
SELECT FORMAT(250500.5634, 0);
Output
250,501