×

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 Stored Procedure

MySQL creates the "stored procedure" function to operate database information. You can use parameters, blocks, and statements to create a new procedure. The procedure requires a database table to use...

3 minutes read.

MySQL Unique index

It helps to maintain data integrity to enforce the uniqueness of values in one or more columns. We can create more than one UNIQUE index in a single table, which...

3 minutes read.

MySQL NOW() function

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

2 minutes read.

MySQL LOG() function

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

3 minutes read.

MySQL CEIL() function

In this context, we will learn how we can use the MySQL CEIL () function with proper syntax and good examples. Introduction of MySQL CEIL() function Input is taken by the CEIL()...

3 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 Truncate() Function

In this context, we will learn how we can use the MYSQL TRUNCATE function to truncate a number to a mentioned number of decimal places. Syntax of MySQL Truncate Function A number...

2 minutes read.

MySQL SEC_TO_TIME() function

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

2 minutes read.

MySQL Operators

MySQL Operators MySQL operator needs advanced operation on the table and its data. This operator works with the "WHERE" clause. MySQL operators are a statement to modify information. It helps to...

13 minutes read.

MySQL Index

An index is a widely used method to access table information quickly. MySQL requires an index for operating table data stored in rows and columns. It's an entry point of...

3 minutes read.

MySQL Environmental Setup

The MySQL Environmental Setup MySQL is free, open-source, and cross-platform software, which can be downloaded from its official website. MySQL management system installs on Linux, macOS, and windows. MySQL requires a framework...

6 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 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 DELETE JOIN

Sometimes join becomes complicated and unwanted. In such cases, we can delete the unwanted joins in the tables. You can delete inner join, left join, right join as per requirement....

3 minutes read.

MySQL Union

MySQL contains the same categories of data in a different table. Sometimes, you require multiple tables to collect data together. MySQL union is a function to combine two table's data...

5 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 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 DATE_ADD() function

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

3 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 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...

2 minutes read.