braincell
braincell

Reputation: 562

BigDecimal in JSTL, divide by int or double returns integer

<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?

Upvotes: 2

Views: 4672

Answers (2)

zybupt
zybupt

Reputation: 1

You can do it like this:

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

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

${a/b}

Upvotes: 0

Sarel Botha
Sarel Botha

Reputation: 12700

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.

Upvotes: 3

Related Questions