Home >Database >Mysql Tutorial >How to Format Integers as Currency in MySQL Queries?

How to Format Integers as Currency in MySQL Queries?

Barbara Streisand
Barbara StreisandOriginal
2024-11-14 17:36:02513browse

How to Format Integers as Currency in MySQL Queries?

Converting Integers to Currency Format in MySQL Queries

In MySQL, you can transform integer values into currency format while performing selections. While you can append the currency symbol using the CONCAT() function, inserting the comma separators for large numbers requires a more elaborate approach.

Using the FORMAT() Function

The FORMAT() function provides a comprehensive solution for formatting numbers, including conversion to currency format.

Syntax:

FORMAT(val, decimals)

Where:

  • val is the integer value you want to convert.
  • decimals specifies the number of decimal places to display.

Example:

To convert the integer 1000 into the currency format "$1,000":

SELECT CONCAT('$', FORMAT(1000, 2)) AS formatted_value;

This query returns the value "$1,000". The FORMAT() function adds the commas as necessary, ensuring a professional currency display.

The above is the detailed content of How to Format Integers as Currency in MySQL Queries?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn