Search code examples
javajsfbigdecimal

BigDecimal rendering the decimal point according to locale?


related to another issue I found out that: if I want to display BigDecimal.ZERO in JSF with 2 fraction digits, then I have to hardcode the rounding in my backing bean. Because numberConverter does not work on the constant.

BigDecimal.ZERO.SetScale(2, RoundingMode.HALF_UP); //this works and displays: "0.00"

Unfortunately I cannot use locale-dependent displaying the decimal point with that! I even cannot change the fractions with min/maxFractionDigits after hardcoding the roundingMode. <f:convertNumber pattern="..." has NOT effect on the display.

This is a real mess, does someone know how to enforce a pattern when displaying a BigDecimal in JSF (not a String! then of course I could use new DecimalFormat).


Solution

  • If the actual format of the textis important - say, you're dealing with locale-dependant data - then you're going to need to use a formatter to render it in a specific 'style'. That's the entire purpose of the class. Using setScale() changes the mathematical precision available to the instance - it doesn't really have any effect (or shouldn't be garuanteed, anyways) about the textual display.