Search code examples
javajsfprimefacesel

How to format a number which is part of the value of p:outputLabel


I have this JSF .xhtml code:

<p:outputLabel value="#{bundle['my.bundle.name']} - #{Myclass.current.amount}"/>

Myclass.current.amount is a double in Java. There is a problem in conversion. For example, if Myclass.current.amount is equal to 10000000, it's displayed like 1.0E7, but I want 10000000.

NOTE: I want a solution in JSF, not in Java.

I tried to use <f:convertNumber/> construction, but it didn't work, and this is not what I need in principle.


Solution

  • You can use f:convertNumber only on components containing a number. If your component value is Some text and #{bean.someNumber}, it first evaluates the value, and the resulting string (for example Some text and 1.0E7) cannot be formatted using f:convertNumber.

    It's not documented, but you can p:outputLabel without the value attribute, but use the body instead. So:

    <p:outputLabel for="...">Label value</p:outputLabel>
    

    This allows you to place the number in its own component and format it:

    <p:outputLabel for="...">
      Some text
      <h:outputText value="#{testView.amount}">
        <f:convertNumber pattern="#"/>
      </h:outputText>
    </p:outputLabel>