Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BigDecimal with 2 fraction after decimal point?

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?

like image 364
membersound Avatar asked Jan 18 '23 10:01

membersound


2 Answers

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" />
like image 86
phanneman Avatar answered Jan 21 '23 16:01

phanneman


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.

like image 41
Thomas Avatar answered Jan 21 '23 16:01

Thomas