javajsfoutputformat

Can I use f:convertNumber with h:outputFormat


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?


Solution

  • <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>