1

I have a composite component and this is a snippet from it.

<h:outputFormat id="output" value="{0} / {1} / {2}" rendered="#{cc.attrs.readOnly}" styleClass="#{cc.attrs.styleClass}">
    <f:param value="#{empty cc.attrs.value1 ? '-' : cc.attrs.value1}" />
    <f:param value="#{empty cc.attrs.value2 ? '-' : cc.attrs.value2}" />
    <f:param value="#{empty cc.attrs.value3 ? '-' : cc.attrs.value3}"/>
</h:outputFormat>

If I want to format value3 using <f:convertNumber>, how can I do that?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Shady Hussein
  • 513
  • 8
  • 24

1 Answers1

5

<h:outputFormat> uses under the covers standard java.text.MessageFormat API. Go ahead clicking that link and reading the javadoc.

<f:convertNumber> uses under the covers the standard java.text.NumberFormat API which happens to be supported by MessageFormat as well. As its javadoc says, numeric patterns can be represented by {[index], number, [pattern]}.

Thus, so (this example assumes that you want 2 fixed-length fraction digits):

<h:outputFormat id="output" value="{0} / {1} / {2,number,#.00}" rendered="#{cc.attrs.readOnly}" styleClass="#{cc.attrs.styleClass}">
    <f:param value="#{empty cc.attrs.value1 ? '-' : cc.attrs.value1}" />
    <f:param value="#{empty cc.attrs.value2 ? '-' : cc.attrs.value2}" />
    <f:param value="#{empty cc.attrs.value3 ? '-' : cc.attrs.value3}"/>
</h:outputFormat>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • That is a great piece of information you give me here, but unfortunately I was to use the grouping property of the convertNumber. Is that possible ? – Shady Hussein Jan 15 '15 at 14:41
  • Just add/remove `,` if necessary to enable/disable grouping. – BalusC Jan 15 '15 at 15:17
  • your answered helped me to develop my answer, unfrotunately i get an exception when I do that. The solution was to make the same check in the value of the outputformat and choose between "number" or simply empty – Shady Hussein Feb 11 '15 at 10:17