Can I use f:convertNumber with h:outputFormat

巧了我就是萌 提交于 2019-12-08 07:13:02

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/27965033/can-i-use-fconvertnumber-with-houtputformat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!