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
以上是如何在 MySQL 查詢中將整數格式化為貨幣?的詳細內容。更多資訊請關注PHP中文網其他相關文章!