Introduction to MySQL FORMAT
The following article provides an outline for MySQL FORMAT. FORMAT function is used to format a numerical value in the table to any desired decimal points. Suppose the table columns have varying decimal points as to one rounded off to 2 places, another column rounded off to 4 places. And in the resulting output, we have to get a product of these two columns. In such cases, the output can have varying decimal places. Each row possessing different decimal points from each other can cause much ambiguity. So, in the result SELECT query, we can directly use the FORMAT function and specify to how many decimal points the output should be rounded off to. This is valid in scenarios where the output variable is not a pre-defined one.
Syntax of MySQL FORMAT
FORMAT function is used to format any numerical value in our output.
The syntax for this function will be as follow:
FORMAT (column_name, decimal_points)
Here, the column_name is the field that is to be rounded off and the decimal_points is to how many places the value be rounded off to.
On a detailed note, the syntax can include one more optional field.
FORMAT (column_name, decimal_points, locale)
Here, the field locale refers to the separators and groupings. This is an optional field and is not defined explicitly, then, en_US locale is used by default. More than 100 locales are defined in MySQL.
How FORMAT Function works in MySQL?
Given below shows how the FORMAT function works in MySQL:
Example #1
Let’s format a value with more decimal places to just two.
Code:
SELECT FORMAT(1500.26543,2) value;
The query says to round off the value 1500.26543 to 2 places instead of 5 decimal places. Hope you are familiar with the principles of rounding off as to consider values above 5 and below 5. So here, the expected output is 1500.27 (because the third digit after the decimal point is 5).
Output:
Example #2
Let’s see another example as below.
Code:
SELECT FORMAT(1500.26111,2) value;
So here, the third digit after the decimal point is less than 5, and thus the expected output is 1500.26.
Output:
Example #3
The FORMAT function also adds extra zeros to extend the decimal places.
Code:
SELECT FORMAT(1500.2,2) value;
The value is 1500.2 and has only one decimal place. But the result is to have 2 decimal places. So, an extra zero will be appended by this query.
Output:
Example #4
Let’s now consider a table inventory with data as No. of items in stock and rate per unit for each item. The result output in this scenario is the total stock value which will be a product of No. of units and rate per unit for each item in stock.
Let’s consider the table as below:
Code:
select * from inventory;
Output:
We will first see the result without using the FORMAT function.
Code:
SELECT itemName, instockValue * PricePerItem ProductCost FROM inventory;
The result output is to display itemName and ProductCost columns where the ProductCost column is a product of inStockValue and PricePerItem.
Output:
So, the output will look like this. It certainly looks clumsy and very inappropriate.
We can use the FORMAT function and retrieve a better-looking output now.
Code:
SELECT itemName, FORMAT (instockValue * PricePerItem, 2) ProductCost
FROM inventory;
We have specified the decimal places to be rounded off to 2 in this case.
Output:
If we compare the outputs of this query and the previous query, we can understand that the latest output is of more clarity.
We can round off the decimal places to zero also.
Code:
SELECT itemName, FORMAT (instockValue * PricePerItem, 0) ProductCost
FROM inventory;
Output:
Here, the output is rounded off to zero decimal places. We can specify the decimal places to be 0, or if we leave it blank, the default value considered is 0.
Example #5
We spoke about using locale in the FORMAT query. By default, the locale is en_US. Let’s try any other locale and identify the difference.
We will see two examples for locales.
a. This query is to update the output as Spanish in Spain.
Code:
SELECT itemName, FORMAT (instockValue * PricePerItem, 2, 'es_ES') ProductCost
FROM inventory;
Output:
b. The decimal point is replaced by comma ‘,’ instead of dot ‘.’ in this locale. And there is no grouping thousands in this format.
Let’s try another locale
Code:
SELECT itemName, FORMAT (instockValue * PricePerItem, 2, 'de_DE') ProductCost
FROM inventory;
Output:
In this locale, the comma (,) is used as decimal point and dot (.) to group thousands. This is reverse of default locale en_EN.
Conclusion
In this article, we saw the syntax and examples of FORMAT function. FORMAT function is used to display an output in the desired format with proper decimal places defined. This is used in the SELECT query and the syntax includes the numeric to be formatted, the number of decimal places, and optionally the locale can also be specified. An important point to be noted is that the data type of the values is to be decimal. Because in INT data type, the decimal places already do not hold valid. The default locale value is en_EN.
Recommended Articles
This is a guide to MySQL FORMAT. Here we discuss the introduction along with how FORMAT function works in MySQL? respectively. You may also have a look at the following articles to learn more –