how to format currency in displaytag

Rakesh Juyal picture Rakesh Juyal · Jun 22, 2009 · Viewed 17.3k times · Source

I am getting the value of amount like 4567.00 , 8976.00 etc. Now while dispalying this value in displaytag i would like to print it as $4567.00 instead of just 4567.00. How can i do that? Provided i just want to use display tag. I can acheive the same thing using core:out tag.

$<core:out value="${variableInMyList}" />

Answer Found [ How i did it ]

Create a new class:

public class NumberFormatDecorator implements DisplaytagColumnDecorator{
    Logger logger = MyLogger.getInstance ( );

    public Object decorate(Object columnValue, PageContext pageContext, MediaTypeEnum media) throws DecoratorException {        
        try
        {
            Object colVal = columnValue;
            if ( columnValue != null ){
                colVal = Double.parseDouble( (String)columnValue );
            }
            return colVal;
        }catch ( Exception nfe ){}
        logger.error( "Unable to convert to Numeric Format");
        return columnValue; // even if there is some exception return the original value
    }
}

now in display tag

<displaytag:column title="Amount" property="amount" decorator="com.rj.blah.utils.decorator.NumberFormatDecorator" format="$ {0,number,0,000.00}"/>

Note: we can use the MessageFormat in format attribute of displaytag:column

Answer

Triqui picture Triqui · Jul 23, 2009

What do you need your class for? You could write it as follows:

<displaytag:column property="amount" format="$ {0,number,0,000.00}"/>