×

MySQL FORMAT() function

In this context, we will learn how we can use the MySQL FORMAT() function with proper syntax and good examples.

Introduction of MySQL FORMAT() function

This function in MySQL helps to format the given number like '#, ###, ###.##", round them to certain decimal points, and returns the result in the form of a string.

Syntax of the MySQL FORMAT() function

The syntax of the MySQL FORMAT() function is given as follows:

FORMAT(M, DEC, locale)

Parameters or arguments used in MySQL FORMAT() function:

There are three parameters accepted by the FORMAT() function in MySQL, which are given as follows:

1. M –

The number that is to be formatted.

2. DEC –

This is the number of decimal places to which the number is rounded off.

3. locale –

It's an optional parameter that decides a thousand separators and grouping between separators. By default, en_US locale is present in MySQL.

Returns:

The function will return the given number with proper format, round it off to a certain decimal place, and return the number in the form of a string.

Applications used for FORMAT() function:

The FORMAT() function can be used in the given below MySQL versions.:

  • MySQL 5.7
  • MySQL 5.6
  • MySQL 5.5
  • MySQL 5.1
  • MySQL 5.0
  • MySQL 4.1
  • MySQL 4.0
  • MySQL 3.23

Examples of MySQL FORMAT() function:

Now we will look into some MySQL FORMAT() function examples and will explore how to use the FORMAT function in MySQL.

Example-1 :

In this example, we will round off the given number by using the FORMAT() function in MySQL to 2 decimal places.

SELECT FORMAT(555454.12365, 2) AS form;

Output :

form
555, 454.12
1 row in set (0.00 sec)

Example 2:

In this example, we will round off the given number by using MySQL's FORMAT() function with 0 decimal places.

SELECT FORMAT(130919999.456, 0)
AS form;

Output :

form
130, 919, 999
1 row in set (0.00 sec)

Example-3 :

Now we will Replace the en_US locale with the de_D locale.

SELECT FORMAT(27112020.1052, 3, 'de_DE') As form;

Output :

form
27.112.020, 105
1 row in set (0.00 sec)

Example-4 :

When we require to round off the column data, then we can take the help of the FORMAT() function in MySQL.

CREATE TABLE Commodity(
Commodity_Id INT AUTO_INCREMENT,
Commodity_Name VARCHAR(100) NOT NULL,
Price INT NOT NULL,
PRIMARY KEY(Commodity_Id )
);

Inserting values into the table:

INSERT INTO Commodity(Commodity_Name, Price)
VALUES
('MotorolaMobile', 75000.999 ),
('SmartWatch,' 73000.455 ),
('Camera,' 170000.545 ) ;

The table will look as follows.

SELECT * FROM Commodity;
Commodity_IdCommodity_NamePrice
1MotorolaMobile75000.999
2Smartwatch73000.455
3Camera170000.545

Now we will format the Price column by rounding up to 1 decimal place.

SELECT
Commodity_Name, FORMAT(Price, 1) As New_price
FROM
Commodity;

Output:

Commodity_NameNew_price
MotorolaMobile75, 001.0
Smartwatch73, 000.5
Camera170, 000.5

Application of MySQL FORMAT() function:

This function is used to format the given number like '#, ###, ###.##", round them to specific decimal points, and returns the result in the form of a string.

Summary:

In the above context, we have learned how we can use the FORMAT() function in MySQL used to format given number like '#, ###, ###.##", round them to specific decimal points, and returns the result in the form of a string.


Related Topics

MySQL Advance function

The advance function operates numerical values, string values, and data types.  The advance function converts, displays, and compares given values as per requirement. Here, you find out database, table, and...

4 minutes read.

MySQL RTRIM() function

In this context, we will learn how we can use the MySQL RTRIM() function with proper syntax and good examples. Introduction of MySQL RTRIM() function It is the function in MySQL that...

2 minutes read.

MySQL Advance table Query

MySQL Advance table Query The table is created by index, rows, and columns in the MySQL database. The user saves their information in matrix format. The database requires a query to...

14 minutes read.

MySQL Vs MongoDB

What is MongoDB? MongoDB is a distributed, open-source, cross-platform document-based database which was created to scale and develop applications simply. It was created as a NoSQL database by MongoDB Inc. MongoDB gets...

6 minutes read.

MySQL logical conditions

MySQL logical conditions Introduction MySQL handles data with clauses, operators, and conditions. The logical condition is used to compare information and returns the required output. This condition applies logic to MySQL expressions...

7 minutes read.

MySQL QUARTER() function

In this context, we will learn how we can use the MySQL QUARTER() function with proper syntax and good examples. Introduction of MySQL QUARTER() function The QUARTER() function in MySQL returns the...

3 minutes read.

MySQL Show index

This query helps to access and retrieve table information. This syntax displays the required index or all indexes of the table. It also displays the index type as per the...

3 minutes read.

MySQL PERIOD_DIFF() function

In this context, we will learn how we can use the MySQL PERIOD_DIFF() function with proper syntax and good examples. Introduction of MySQL PERIOD_DIFF() function This function in MySQL is used to...

2 minutes read.

MySQL LN() function

In this context, we will learn how we can use the MySQL LN() function with proper syntax and good examples. Introduction of MySQL LN() function For evaluation of the natural logarithm of...

2 minutes read.

MySQL MOD() function

In this context, we will learn how we can use the MySQL MOD() function with proper syntax and good examples. Introduction of MySQL MOD() function Basically, in MySQL, the MOD() function is...

3 minutes read.

MySQL TAN() Function

Basically, the TAN() function in MySQL is used to give back the tangent of a mentioned number.In any kind of right-angle triangle, the tangent of an angle is the length...

2 minutes read.

MySQL AVG function

This function works on numerical data type values. The average function shows the average value of the data set. If an average function returns a null value, then the row...

5 minutes read.

MySQL RIGHT JOIN

MySQL right join links two tables with each other. The left table column connects with the complete right side table. Each row of the right table tries to connect with...

4 minutes read.

MySQL LIKE() function

In this context, we will learn how we can use the MySQL LIKE() function with proper syntax and good examples. Introduction of MySQL LIKE() function The LIKE function is a logical function...

4 minutes read.

MySQL vs SQL

What is MySQL? The open-source MySQL relational database management system is a vital software component for web-based applications. As the data is saved and sent over the internet, databases and related...

6 minutes read.

MySQL INSERT() Function

In this context, we will learn how we can use the MySQL INSERT() function with proper syntax and good examples. Introduction of MySQL INSERT() function Basically, in MySQL, the INSERT function is...

2 minutes read.

MySQL Date and Time function

The date function displays day, year, month, time, and current date. It shows the date and time as per the requirement of the applications. The data either store on the...

8 minutes read.

MySQL Database Introduction

MySQL - Database Introduction: The database plays an essential role in MySQL for storing and modifying the data. The database creates and keeps multiple tables. The database organizes and manipulates...

8 minutes read.

MySQL RADIANS() function

In this context, we will learn how we can use the MySQL RADIANS() function with proper syntax and good examples. Introduction of MySQL RADIANS() function RADIANS() function in MySQL is used to...

2 minutes read.

MySQL Subquery

The subquery is a MySQL query used to manage data operations. Mainly subquery helps to retrieve data with the necessary condition. It creates a nested query with two different queries....

5 minutes read.