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

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

Barbara Streisand
Barbara Streisand原創
2024-11-14 17:36:02400瀏覽

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.

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

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn