Displaying a number in 2 point decimal format in jsf

允我心安 提交于 2019-11-27 22:12:13

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.

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