Search code examples
javajspformatlocaledisplaytag

Displaytag format tag currency


Im using displaytag 1.2 and fmt tags:

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://displaytag.sf.net" prefix="display" %>

Im trying to use displaytags format-attribute to format a number to a currency. However it seems as displaytag renders the currency-format different than fmt-numberformat. This causes inconsistens in my page and needs to be resolved. Ex. Displaytag formats local currency like this: NOK 1,500.00 Ex. FMT formats local currency like this: kr 1 500,00

The FMT version is correct, so i would like to use the same format in displaytag. How can i get displaytag to format in the correct way?

Preferably without decorators.

This is my current jsp-code snippets:

<fmt:formatNumber type="currency"><s:property value="reportFooter.pris" /></fmt:formatNumber>
<display:column property="pris" sortable="true" titleKey="report.header.price" format="kr {0,number,currency}"/>

Solution

  • This is a cheap test:

    int i = 1000;
    MessageFormat mf = new MessageFormat("{0,number,currency}", new Locale("no", "NO"));
    System.out.println(mf.format(new Object[] {new Integer(i)}));
    

    Prints out kr 1 000,00

    int i = 1000;
    MessageFormat mf = new MessageFormat("{0,number,currency}", new Locale("en", "NO"));
    System.out.println(mf.format(new Object[] {new Integer(i)}));
    

    Prints out NOK1,000.00

    Your problem is that you did not configure displaytag's LocaleResolver correctly. It has Norway locale but with English language.

    Displaytag's code is fine, it does exactly the same as I do in my test code:

    50  public MessageFormatColumnDecorator(String pattern, Locale locale)
    51 {
    52 this.format = new MessageFormat(pattern, locale);
    53 }

    58 public Object decorate(Object columnValue, PageContext pageContext, MediaTypeEnum media)
    59 {
    60 try
    61 {
    62 return this.format.format(new Object[]{columnValue});
    63 }
    64 catch (IllegalArgumentException e)