17

I am using JSF 2 and RichFaces 3. Here in the picture shown below, numbers are being displayed as what they are in the database.

enter image description here

But I want to display them as 6749395.20 if fraction part is there and 5095138.00 if no fraction part is there.

As of now I have tried something like this.

 <rich:column>
    <f:facet name="header">
        <h:outputText value="Total Amount"/>
    </f:facet>
    <h:outputText value="#{rr[2]}">
        <f:convertNumber type="number" groupingUsed="true" minFractionDigits="2" pattern="#0.00"/>
    </h:outputText>
 </rich:column>

Actually I am showing all of them together, but I have tried with all of them as all possible combinations with type, groupingUsed, minFractionDigits and pattern.

Why does it not work? How is this caused and how can I solve it?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Addicted
  • 1,694
  • 1
  • 16
  • 24
  • Your converter should work as expected. The `minFractionDigits="2"` should be enough. Did you try the same outside the dataTable? – Matt Handy May 02 '12 at 12:29
  • ya man I tried it outside the datatable also, but the main problem was datatype and thanks to @BalusC, He helped me out. – Addicted May 03 '12 at 05:29

1 Answers1

44

That can happen if the value is not a Number at all, for example a String. You're then basically using the wrong type for the data it represents. To represent currencies in Java, you should be using BigDecimal. Also, make sure that the type in the database table is right, i.e. it should not be a varchar, but a decimal.

Once you've fixed the data type, then the <f:convertNumber> will work as you told it to do. Note that the pattern attribute will override the groupingUsed and minFractionDigits. You should use either the pattern or the others. Also, type="number" is already the default, so it can be removed.

So, either use

<f:convertNumber pattern="#0.00" />

or

<f:convertNumber groupingUsed="true" minFractionDigits="2" />

Note that they generate different formats. You probably want to set grouping to false.

You can also use type="currency", it will then automatically apply the right pattern as per the UIViewRoot#getLocale():

<f:convertNumber type="currency" />

See also the tag library documentation and the DecimalFormat javadoc.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • actually in my database they were numbers and i was fetching them in string and then was trying to display them as number with some pattern. Now I have changed it to an arraylist. and now it is displaying the correct and desired format. Thanks Buddy Thanks a lot................ – Addicted May 03 '12 at 05:26
  • And with name currency, is there any method which I can display INR symbol along with rupee like I CAN display Dollar($) symbol. – Addicted May 03 '12 at 05:28