Java Struts 2 <s:property/> 标签中的格式编号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1558662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Format number in Struts 2 <s:property/> tag
提问by prostynick
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
?
我想格式化<s:property value="summary.total"/>
Struts 2中标签显示的数字。有一个double
值。我怎样才能做到这一点?我应该使用OGNL
吗?
Or maybe I must use <s:text/>
tag and define my format in resuource file?
或者我必须使用<s:text/>
标签并在资源文件中定义我的格式?
采纳答案by Trick
You need to use <s:text/>
with <s:param/>
.
您需要<s:text/>
与<s:param/>
.
Property file:
属性文件:
summary.cost= {0,number,##0.00}
summary.cost= {0,number,##0.00}
JSP:
JSP:
<s:text name="summary.cost">
<s:param name="value" value="summary.total"/>
</s:text>
Thisanswerexplains how to use #
and 0
in the format mask.
这个答案解释了如何使用#
和0
格式掩码。
回答by Alfredo Osorio
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
希望这有帮助
回答by Juanmi
The way more fast
方式更快
<s:property value="getText('{0,number,#,##0.00}',{summary.total})"/>
Lucky!!
幸运的!!
回答by sarie
i had this problem to format a number in this way 1.234,56
我有这个问题以这种方式格式化数字 1.234,56
so i prefered both tags struts tag and fmt tag(fmt because s:number don't exist)
所以我更喜欢标签 struts 标签和 fmt 标签(fmt 因为 s:number 不存在)
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
这是工作
回答by Alireza Fattahi
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
如果您的属性在您的操作中不是数字,那么 getText 将无法处理它。该模式仅接受数字。在这种情况下,您可以使用@sarie 提到的 fmt
<fmt:formatNumber groupingUsed="true" type="currency" value="${amount}" />
回答by Yan Pak
The most quickiest and easiest way is to use <s:number />
tag.
最快捷、最简单的方法是使用<s:number />
标签。
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://struts.apache.org/maven/struts2-core/apidocs/org/apache/struts2/components/Number.html