16

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?

skaffman
  • 398,947
  • 96
  • 818
  • 769
prostynick
  • 6,129
  • 4
  • 37
  • 61

6 Answers6

18

The way more fast

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

Lucky!!

Juanmi
  • 181
  • 1
  • 2
10

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.

Community
  • 1
  • 1
Trick
  • 3,779
  • 12
  • 49
  • 76
5

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

Alfredo Osorio
  • 11,297
  • 12
  • 56
  • 84
3

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

sarie
  • 31
  • 1
2

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}" />
Alireza Fattahi
  • 42,517
  • 14
  • 123
  • 173
0

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

Yan Pak
  • 1,767
  • 2
  • 19
  • 15