I'd like to have a BigDecimal formatted with 2 decimals after the decimal point. Always.
Based on some boolean checks, I sometimes return a BigDecimal.ZERO;
In this case, the JSF page just displays "0", although I set <f:convertNumber minFractionDigits="2" maxFractionDigits="2"/>
.
I do not understand this, but nevertheless how can I enforce 2 digits else?
there are 2 ways of doing this
you can use the built in BigDecimal
function
myBigDecimal.SetScale(2, RoundingMode.HALF_UP);
This returns a big decmial with 2 digits past the decimal and also rounds up.(there are also other rounding methods)
OR
you could use jsf convertNumber Tag
<f:convertNumber maxFractionDigits="2" minFractionDigits="2" />
Try formatting the number like this:
<h:outputText value="#{myBean.bd1}">
<f:convertNumber pattern="0.00" />
</h:outputText>
Note: Please also pay attention to the warnings regarding to rounding in the accepted answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With