位置:首頁 > Java技術 > JSP教學 > JSTL <fmt:formatNumber>標簽

JSTL <fmt:formatNumber>標簽

<fmt:formatNumber>標簽用於格式化數字,百分比和貨幣。

屬性:

<fmt:formatNumber>標簽具有以下屬性:

屬性 描述 必須? 默認
value Numeric value to display Yes None
type NUMBER, CURRENCY, or PERCENT No Number
pattern Specify a custom formatting pattern for the output. No None
currencyCode Currency code (for type="currency") No From the default locale
currencySymbol Currency symbol (for type="currency") No From the default locale
groupingUsed Whether to group numbers (TRUE or FALSE) No true
maxIntegerDigits Maximum number of integer digits to print No None
minIntegerDigits Minimum number of integer digits to print No None
maxFractionDigits Maximum number of fractional digits to print No None
minFractionDigits Minimum number of fractional digits to print No None
var Name of the variable to store the formatted number No Print to page
scope Scope of the variable to store the formatted number No page
  • 如果type屬性是百分比或數字,那麼您可以使用多個數字格式屬性。maxIntegerDigits和minIntegerDigits屬性允許你指定數量的nonfractional部分的大小。如果實際數目超過maxIntegerDigits,電話號碼將被截斷。

  • 還提供了屬性,讓你確定有多少小數位應該被使用。該minFractionalDigits和maxFractionalDigits屬性允許你指定的小數位數。如果數目超過小數的最大位數,該數字將四舍五入。

  • 分組可以用來插入數千組逗號之間。分組是通過設置groupingIsUsed屬性為true或false指定。當使用minIntegerDigits分組,要小心得到預期的結果。

  • 你可以選擇要使用的模式屬性。這個屬性可指定希望的格式,如電話號碼編碼特殊字符。下表顯示了這些代碼。

Symbol 描述

0

Represents a digit.

E

Represents in exponential form.

#

Represents a digit; displays 0 as absent.

.

Serves as a placeholder for a decimal separator.

,

Serves as a placeholder for a grouping separator.

;

Separates formats.

-

Used as the default negative prefix.

%

Multiplies by 100 and displays as a percentage.

?

Multiplies by 1000 and displays as per mille.

¤

Represents the currency sign; replaced by actional currency symbol.

X

Indicates that any other characters can be used in the prefix or suffix.

'

Used to quote special characters in a prefix or suffix.

實例:

<%@ 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>
<p>Formatted Number (5): <fmt:formatNumber type="percent" 
            maxIntegerDigits="3" value="${balance}" /></p>
<p>Formatted Number (6): <fmt:formatNumber type="percent" 
            minFractionDigits="10" value="${balance}" /></p>
<p>Formatted Number (7): <fmt:formatNumber type="percent" 
            maxIntegerDigits="3" value="${balance}" /></p>
<p>Formatted Number (8): <fmt:formatNumber type="number" 
            pattern="###.###E0" value="${balance}" /></p>
<p>Currency in USA :
<fmt:setLocale value="en_US"/>
<fmt:formatNumber value="${balance}" type="currency"/></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

Formatted Number (5): 023%

Formatted Number (6): 12,000,023.0900000000%

Formatted Number (7): 023%

Formatted Number (8): 120E3

Currency in USA : $120,000.23