BigDecimal with 2 fractions after the decimal point?

I would like to have a BigDecimal formatted with 2 decimal places after the decimal point. Is always.

Based on some logical checks, I sometimes return BigDecimal.ZERO; In this case, only “0” is displayed on the JSF page, although I set <f:convertNumber minFractionDigits="2" maxFractionDigits="2"/> .

I do not understand this, but nonetheless, how can I enter 2 digits yet?

+4
source share
3 answers

there are two ways to do this

you can use the built-in BigDecimal function

 myBigDecimal.SetScale(2, RoundingMode.HALF_UP); 

This returns a large decmial with 2 digits per decimal and also rounds up (there are other rounding methods)

OR

you can use jsf convertNumber tag

 <f:convertNumber maxFractionDigits="2" minFractionDigits="2" /> 
+8
source

Try formatting the number as follows:

  <h:outputText value="#{myBean.bd1}"> <f:convertNumber pattern="0.00" /> </h:outputText> 

Note. Also note rounding warnings in the accepted answer.

+1
source

try it

 <af:convertNumber maxIntegerDigits="3" maxFractionDigits="2" minFractionDigits="2" pattern="###.##" /> 
0
source

Source: https://habr.com/ru/post/1397913/


All Articles