jsf 1.1 中的 BigDecimal 舍入
jsf 1.1 使用的 el-1.0 中存在一个错误,该错误将 BigDecimal 转换为 double,然后在更新模型阶段返回 BigDecimal。这发生在 ELSupport.java -> coerceToNumber 类并在 el-impl-2.1.2-b03 中修复。最终结果是,如果用户输入 54.93,那么当 updateModel 阶段发生时,结果将是 54.92999999...。
假设我没有升级到 el-2.1.2-b03 的选项,但仍然必须保证我的计算和发送到后端的内容的准确性。使用的最佳舍入策略是什么?我正在考虑只要在 updateModel 阶段在我的支持 bean 中调用 setter 方法,就可以将比例设置为 2,并采用舍入策略 ROUND_HALF_EVEN 。
有更好的解决方案吗?
There is a bug in el-1.0 which jsf 1.1 uses which converts a BigDecimal to double and then back to BigDecimal during the update model phase. This occurs in the ELSupport.java -> coerceToNumber class and was fixed in el-impl-2.1.2-b03. The end result is that if a user enters 54.93, then it results in 54.92999999... when the updateModel phase occurs.
Assume that I do not have the option to upgrade to el-2.1.2-b03 but still must guarantee accuracy of my calculations and what I send to the backend. What is the best rounding strategy to use? I was thinking of simply setting the scale to 2 with a rounding strategy of ROUND_HALF_EVEN whenever the setter method is called in my backing bean during the updateModel phase.
Is there a better solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JSF 1.1 中没有解决这个问题。两种解决方法是:
绑定到字符串并通过调用 new BigDecimal(string) 转换为 BigDecimal;
继续绑定到 BigDecimal,但附加一个 ValueChangeListener,您可以在调用 FacesContext 上的 renderResponse 之前在数据模型中手动设置值。您必须通过调用 renderResponse 来跳过更新模型阶段,否则这将不起作用。
There is no solution for this in JSF 1.1. The two work arounds are to:
Bind to a String and convert to BigDecimal by calling new BigDecimal(string);
Continue binding to BigDecimal but attach a ValueChangeListener where you can manually set the Value in your datamodel prior to calling renderResponse on FacesContext. You must skip the update model phase by calling renderResponse or this will not work.