Home >Database >Mysql Tutorial >How can I format integers as currency in MySQL queries?

How can I format integers as currency in MySQL queries?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-15 08:25:02920browse

How can I format integers as currency in MySQL queries?

Formatting Integers as Currency using MySQL Queries

When working with integers representing monetary values in MySQL, you may encounter the need to format them into currency format. This is beneficial for improving the readability and user experience of your applications. Here are a couple of approaches to achieve this:

1. CONCAT Function

You can combine the CONCAT() function to append the currency symbol '$' and a comma-separated number format to the integer. However, it requires some string manipulation:

SELECT CONCAT('$', FORMAT(val, 2)) ... ;

2. FORMAT() Function

MySQL provides a versatile FORMAT() function specifically designed for formatting numbers. It allows you to specify the decimal precision and apply formatting options, including currency symbols and commas:

SELECT FORMAT(val, 2, 'en_US') ... ;

In this example, 'en_US' is the locale identifier, which influences the currency format based on the regional settings. You can find a list of supported locales here:

http://dev.mysql.com/doc/refman/5.5/en/locale-support.html

The above is the detailed content of How can I 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