BigDecimal rounding in jsf 1.1

There is an error in el-1.0 used by jsf 1.1 that converts BigDecimal to double and then back to BigDecimal during the update model phase. This happens in the class ELSupport.java → coerceToNumber and is fixed in el-impl-2.1.2-b03. The end result is that if the user enters 54.93, then this leads to 54.92999999 ... when the updateModel step occurs.

Suppose that I do not have the opportunity to upgrade to el-2.1.2-b03, but I still have to guarantee the accuracy of my calculations and what I send to the server. What is the best rounding strategy? I thought of simply setting the scale to 2 using the ROUND_HALF_EVEN rounding strategy whenever the setter method is called in my bean support during the updateModel phase.

Is there a better solution?

+3
source share
1 answer

There is no solution for this in JSF 1.1. These two tasks:

  • Bind to String and convert to BigDecimal by calling the new BigDecimal (string);

  • Continue linking with BigDecimal, but attach a ValueChangeListener where you can manually set the value in your model date before calling renderResponse on the FacesContext. You must skip the update model phase by calling renderResponse, or it will not work.

+1
source

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


All Articles