Tuesday, October 1, 2013

Thousands or comma separator in JSF

Are you working with a financial application and now thinking about formatting your result with thousand separator ? Then don't worry. Don't even think about using javascript or whatever folks out there. In fact the converter offered by jsf make it easy to the job and even more given the list of other attributes available.

Suppose your total is declared as follows:

BigDecimal annualBugdet;

your setter and getter may also be defined :

public void setAnnualBugdet( BigDecimal annualBugdet) {

 this.annualBugdet = annualBugdet.setScale(2,RoundingMode.HALF_EVEN);
}


public BigDecimal  getAnnualBugdet( BigDecimal annualBugdet) {


 return annualBugdet;
}


your bean is then called from the view as :

<h:outputText value="#{yourBean.annualBugdet}" > <f:convertNumber groupingUsed="true" maxFractionDigits="2" currencySymbol="$" > </h:outputText>

No comments:

Post a Comment