如何在JSP中格式化數字?
<fmt:formatNumber>標籤用於格式化數字、百分比和貨幣。
屬性
<fmt:formatNumber>標籤具有以下屬性:
屬性 | 描述 | 必填 | 預設值 |
---|---|---|---|
值 | 要顯示的數值 | 是 | 無 |
型別 (type) | NUMBER、CURRENCY 或 PERCENT | 否 | 數字 (Number) |
模式 (pattern) | 指定輸出的自定義格式模式。 | 否 | 無 |
貨幣程式碼 (currencyCode) | 貨幣程式碼(對於 type = "currency") | 否 | 來自預設區域設定 |
貨幣符號 (currencySymbol) | 貨幣符號(對於 type = "currency") | 否 | 來自預設區域設定 |
是否使用分組 (groupingUsed) | 是否對數字進行分組 (TRUE 或 FALSE) | 否 | true |
最大整數位數 (maxIntegerDigits) | 要列印的最大整數位數 | 否 | 無 |
最小整數位數 (minIntegerDigits) | 要列印的最小整數位數 | 否 | 無 |
最大小數位數 (maxFractionDigits) | 要列印的最大小數位數 | 否 | 無 |
最小小數位數 (minFractionDigits) | 要列印的最小小數位數 | 否 | 無 |
變數 (var) | 用於儲存格式化數字的變數名稱 | 否 | 列印到頁面 |
範圍 (scope) | 用於儲存格式化數字的變數範圍 | 否 | 頁面 (page) |
示例
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %> <html> <head> <title>JSTL fmt:formatNumber Tag</title> </head> <body> <h3>Number Format:</h3> <c:set var = "balance" value = "120000.2309" /> <p>Formatted Number (1): <fmt:formatNumber value = "${balance}" type = "currency"/></p> <p>Formatted Number (2): <fmt:formatNumber type = "number" maxIntegerDigits = "3" value = "${balance}" /></p> <p>Formatted Number (3): <fmt:formatNumber type = "number" maxFractionDigits = "3" value = "${balance}" /></p> <p>Formatted Number (4): <fmt:formatNumber type = "number" groupingUsed = "false" value = "${balance}" /></p> </body> </html>
以上程式碼將生成以下結果:
Number Format: Formatted Number (1): £120,000.23 Formatted Number (2): 000.231 Formatted Number (3): 120,000.231 Formatted Number (4): 120000.231
廣告