Format number in Struts 2 <s:property/> tag

前提是你 提交于 2019-12-02 19:27:12
Trick

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.

The way more fast

<s:property value="getText('{0,number,#,##0.00}',{summary.total})"/>

Lucky!!

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

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

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}" />

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!