BigDecimal in JSTL, divide by int or double returns integer

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 03:11:45

问题


<fmt:formatNumber var="instAmount" value="${invoice.amount / offer.getTotalInstallments()}" minFractionDigits="2" />

Where amount is BigDecimal in Java and totalInstallments is int. I tried by setting totalInstallments to double but nothing changes. It returns an Integer, it behaves as when you divide two integers in Java, you get an integer.

Am I missing something or is there a workaround?


回答1:


The easiest solution is to just do this calculation in the controller/servlet and use the result on the view page. I think calculations should be done in the controller and not the view. You would use the divide() method on the BigDecimal.

That said, if you do want to do this in the view you have to define a JSTL function in a tag library and create a static version of divide which accepts the BigDecimal and the int you are diving by.




回答2:


You can do it like this:

< fmt:parseNumber value="${invoice.amount}" var="a" />

< fmt:parseNumber value="${offer.value)}"  var="b" />

${a/b}


来源:https://stackoverflow.com/questions/10882549/bigdecimal-in-jstl-divide-by-int-or-double-returns-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!