问题
I would like to format number displayed by <s:property value="summary.total"/>
tag in Struts 2. There is a double
value. How can I do that? Should I use OGNL
?
Or maybe I must use <s:text/>
tag and define my format in resuource file?
回答1:
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.
回答2:
The way more fast
<s:property value="getText('{0,number,#,##0.00}',{summary.total})"/>
Lucky!!
回答3:
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
回答4:
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
回答5:
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}" />
回答6:
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
来源:https://stackoverflow.com/questions/1558662/format-number-in-struts-2-sproperty-tag