Format number in Struts 2 tag

前端 未结 6 1944
梦毁少年i
梦毁少年i 2021-02-01 23:28

I would like to format number displayed by tag in Struts 2. There is a double value. How can I do that? Sho

相关标签:
6条回答
  • 2021-02-01 23:41

    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

    0 讨论(0)
  • 2021-02-01 23:42

    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

    0 讨论(0)
  • 2021-02-01 23:45

    The way more fast

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

    Lucky!!

    0 讨论(0)
  • 2021-02-01 23:47

    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.

    0 讨论(0)
  • 2021-02-01 23:49

    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}" />
    
    0 讨论(0)
  • 2021-02-01 23:58

    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

    0 讨论(0)
提交回复
热议问题