I would like to format number displayed by
tag in Struts 2. There is a double
value. How can I do that? Sho
The most quickiest and easiest way is to use <s:number />
tag.
Example:
<s:number name="%{summary.total}" minimumFractionDigits="2" type="currency" currency="USD" />
More about tag here https://struts.apache.org/maven/struts2-core/apidocs/org/apache/struts2/components/Number.html
i had this problem to format a number in this way 1.234,56
so i prefered both tags struts tag and fmt tag(fmt because s:number don't exist)
so i used the following syntaxe:
<s:label label="mylabel">
<s:param name="value">
<s:text name="">
<fmt:formatNumber maxFractionDigits="2" pattern="#.###" >1234.56</fmt:formatNumber>
</s:text>
</s:param>
</s:label>
and it's work
The way more fast
<s:property value="getText('{0,number,#,##0.00}',{summary.total})"/>
Lucky!!
You need to use <s:text/>
with <s:param/>
.
Property file:
summary.cost= € {0,number,##0.00}
JSP:
<s:text name="summary.cost">
<s:param name="value" value="summary.total"/>
</s:text>
This answer explains how to use #
and 0
in the format mask.
If your property is not number in your action then the getText will not work on it. The pattern accept numbers only. In this case you can go with fmt as mentioned by @sarie
<fmt:formatNumber groupingUsed="true" type="currency" value="${amount}" />
This one is quicker:
<s:property value="getText('struts.money.format', {summary.cost})" />
And in your properties file this:
struts.money.format= {0,number,\u00A4##0.00}
Hope this help