Home > Database > Mysql Tutorial > body text

How to Format Integers as Currency in MySQL Queries?

Barbara Streisand
Release: 2024-11-14 17:36:02
Original
462 people have browsed it

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)
Copy after login

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;
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template