首頁 > 資料庫 > mysql教程 > 如何在 MySQL 查詢中將整數格式化為貨幣?

如何在 MySQL 查詢中將整數格式化為貨幣?

Patricia Arquette
發布: 2024-11-15 08:25:02
原創
932 人瀏覽過

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

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

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板