<fmt:formatNumber> tags are used to format numbers, percentages, and currencies.
Property
The <fmt:formatNumber> tag has the following properties:
Properties |
Description |
whether it is necessary |
Default Value |
Value |
The number to display |
Is |
No |
Type |
Number,currency, or Percent type |
Whether |
Number |
Pattern |
Specifies a custom format pattern to use with the output |
Whether |
No |
CurrencyCode |
Currency Code (when type= "currency") |
Whether |
Depends on the default zone |
CurrencySymbol |
Currency symbol (when type= "currency") |
Whether |
Depends on the default zone |
Groupingused |
Whether to group numbers (TRUE or FALSE) |
Whether |
True |
Maxintegerdigits |
Maximum number of digits of an integer |
Whether |
No |
Minintegerdigits |
Number of digits with the smallest number of integers |
Whether |
No |
Maxfractiondigits |
The maximum number of digits after the decimal point |
Whether |
No |
Minfractiondigits |
The minimum number of digits after the decimal point |
Whether |
No |
Var |
Variables that store formatted numbers |
Whether |
Print to Page |
Scope |
Scope of the Var attribute |
Whether |
Page |
If the type attribute is percent or number, then you can use several other formatting numeric attributes. The Maxintegerdigits property and the Minintegerdigits property allow you to specify the length of an integer. If the actual number exceeds the maximum value specified by Maxintegerdigits, the number is truncated.
There are some properties that allow you to specify the number of digits after the decimal point. The Minfractionaldigits property and the Maxfractionaldigits property allow you to specify the number of digits after the decimal point. If the actual number exceeds the specified range, the number is truncated.
A numeric grouping can be used to insert a comma in every three digits. The Groupingisused property is used to specify whether to use numeric grouping. When used with the Minintegerdigits attribute, it is necessary to be very careful to get the expected results.
You might use the Pattern property. This property allows you to include the specified character when you encode the number. These characters are listed in the following table.
symbols |
Description |
0 |
Represents a single digit |
E |
Use exponential format |
# |
Represents a digit, if not, displays 0 |
. |
Decimal point |
, |
Numeric grouping separators |
; |
Delimited format |
- |
Using the default negative number prefix |
% |
Percentage |
? |
Thousand Fractions |
¤ |
Currency symbol, using the actual currency symbol instead |
X |
Specify a character that can be used as a prefix or suffix |
‘ |
To reference special characters in a prefix or suffix |
Example Demo
<%@ 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:setvar= "Balance"value= "120000.2309" /><P>Formatted number (1):<Fmt:formatnumbervalue= "${balance}"type= "Currency"/></P><P>Formatted Number (2):<Fmt:formatnumbertype= "Number"maxintegerdigits= "3"value= "${balance}" /></P><P>Formatted Number (3):<Fmt:formatnumbertype= "Number"maxfractiondigits= "3"value= "${balance}" /></P><P>Formatted Number (4):<Fmt:formatnumbertype= "Number"groupingused= "false"value= "${balance}" /></P><P>Formatted Number (5):<Fmt:formatnumbertype= "percent"maxintegerdigits= "3"value= "${balance}" /></P><P>Formatted Number (6):<Fmt:formatnumbertype= "percent"minfractiondigits= "Ten"value= "${balance}" /></P><P>Formatted Number (7):<Fmt:formatnumbertype= "percent"maxintegerdigits= "3"value= "${balance}" /></P><P>Formatted Number (8):<Fmt:formatnumbertype= "Number"pattern= "###.## #E0"value= "${balance}" /></P><P>Currency in USA:<Fmt:setlocalevalue= "en_US"/><Fmt:formatnumbervalue= "${balance}"type= "Currency"/></P></Body></HTML>
The results of the operation are as follows:
Number format:formatted number (1): £120,000.23formatted number (2): 000.231formatted Number ( 3): 120,000.231formatted Number (4): 120000.231formatted Number (5): 023%formatted Number ( /C5>6): 12,000,023.0900000000%formatted Number (7): 023%formatted Number (8): 120E3Currency in USA: $120,000.23
<fmt:formatNumber> tags