w3resource logo

MySQL format function

MySQL FORMAT() function

rating MySQL format() function Average rating 6 out of 10. Total 13 users rated.



MySQL FORMAT() converts a number to a format like ‘#,###,###.##’ which is rounded upto the number of decimal places specified (in the second argument) and returns the result as a string. There is no decimal point, if decimal place is defined as 0.




Name Description
N A number which may be an integer, a decimal or a double.
D An integer which specifies up to how many decimal places the return value is going to contain.

MySQL Version : 5.6

Pictorial Presentation

MySQL FORMAT() pictorial presentation

Example : MySQL FORMAT() function

The following MySQL statement calculates up to 3 decimal places of 12324.2573, so it returns 12,324.257.

SELECT FORMAT(12324.2573,3);



Example of MySQL format() function using where clause

The following statement will return those books from the book_mast table, whose prices are more than 150. The price is returned up to 4 decimal places as specified in the argument.

SELECT book_name,FORMAT(book_price,4) 
FROM book_mast
WHERE book_price>150;

Sample table : book_mast



PHP script

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>example-format-function - php mysql examples | w3resource</title>
echo "<h2>list of books and their prices if the book priced more than 150. Prices are formatted upto four places after decimal : </h2>";
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='250' align='center'>Author's id</td><td width='250' align='center'>Prices</td>";
echo "</tr>";
$result = mysql_query("SELECT book_name,FORMAT(book_price,4) 
FROM book_mast 
WHERE book_price>150");
echo "<tr>";
echo "<td align='center' width='200'>" . $row['book_name'] . "</td>";
echo "<td align='center' width='200'>" . $row['FORMAT(book_price,4)'] . "</td>";
echo "</tr>";
echo "</table>";

View the example in browser

All String Functions

MySQL String Functions, slide presentation

We have compiled a number of MySQL Questions and Answers. Check and assess you MySQL skill by visiting those questions and answers.


Rate this tutorial

Your Rating: not set

Share this tutorial

RSS Feed