Jsp-jstl-format-formatnumber-tag
提供:Dev Guides
JSTL-コア<fmt:formatNumber>タグ
*<fmt:formatNumber>* タグは、*数値、パーセンテージ、*、および*通貨*をフォーマットするために使用されます。
属性
*<fmt:formatNumber>* タグには次の属性があります-
Attribute | Description | Required | Default |
---|---|---|---|
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* 属性を使用すると、数値の非小数部分のサイズを指定できます。 実際の数値が *maxIntegerDigits* を超える場合、数値は切り捨てられます。
*また、属性を使用して、使用する小数点以下の桁数を決定できます。* minFractionalDigits *および *maxFractionalDigits* 属性を使用すると、小数点以下の桁数を指定できます。 数値が小数桁の最大数を超える場合、数値は丸められます。
* グループ化を使用して、数千のグループ間にコンマを挿入できます。 グループ化は、 *groupingIsUsed* 属性をtrueまたはfalseに設定することにより指定されます。 *minIntegerDigits* でグループ化を使用する場合、意図した結果が得られるように注意する必要があります。
*パターン属性の使用を選択できます。 この属性を使用すると、番号のエンコード方法を指定する特殊文字を含めることができます。 次の表にコードを示します。
S.No. | Symbol & Description |
---|---|
1 |
数字を表します。 |
2 |
E 指数形式で表します。 |
3 |
# 数字を表します。 0を不在として表示します。 |
4 |
. 小数点のプレースホルダーとして機能します。 |
5 |
, グループ化区切りのプレースホルダーとして機能します。 |
6 |
; フォーマットを区切ります。 |
7 |
- デフォルトの負のプレフィックスとして使用されます。 |
8 |
% 100で乗算し、パーセンテージで表示します。 |
9 |
? 1000倍して、ミルごとに表示します。 |
10 |
¤ 通貨記号を表します。行動通貨記号に置き換えられました。 |
11 |
X プレフィックスまたはサフィックスに他の文字を使用できることを示します。 |
12 |
'' プレフィックスまたはサフィックスの特殊文字を引用するために使用されます。 |
例
<%@ 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