本文介绍了JSTL中的BigDecimal,除以int或double会返回整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

其中Java中的金额为BigDecimal并且totalInstallmentsint的金额.我尝试通过将totalInstallments设置为double,但是没有任何变化.它返回一个Integer,它的行为就像在Java中将两个整数相除时会得到一个整数.

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.

我丢失了什么东西吗?

推荐答案

最简单的解决方案是仅在controller/servlet中进行此计算,然后在视图页面上使用结果.我认为计算应该在控制器而不是视图中完成.您可以在BigDecimal上使用divide()方法.

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.

也就是说,如果您确实想在视图中执行此操作,则必须在标签库中定义一个JSTL函数,并创建一个静态版本的除法,该除法将接受BigDecimal和您要通过的int.

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.

这篇关于JSTL中的BigDecimal,除以int或double会返回整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:52
查看更多