I have a template which iterates through a map and displays the info as such:
#{list items:report.getCategoryMap()?.keySet(), as:'cat'}
%{models.reporting.TransactionReportItem item = report.getCategoryMap()?.get(cat);}%
${cat}
${item?.nbCredit}
${item?.getCreditPerc(report.nbCredit)}
${item?.nbDebit}
${item?.getDebitPerc(report.nbDebit)}
${item?.getTotalTransactions()}
#{/list}
for some reason the template always render the result of getCreditPerc and getDebitPerc as 0.0
public Double getCreditPerc(long totalCredit){
double perc = (double) (nbCredit / totalCredit);
Logger.info("nbCredit: %s, total cr: %s", nbCredit, totalCredit);
return new Double(perc);
}
When calling the template I can see the output in the log:
2011-11-21 13:54:22 INFO ~ [TransactionReportItem:85] getDebitPerc() – nbDebit: 39, total cr: 4984
I tried using primitive type instead of double object with no success.
When debugging the code I can see that all the values are correctly set.
Could it be something to do with the groovy template rendering?
Posting the log from a different function doesn’t help this question along 😉
However, the problem is that you have two
intsin Java, and you are dividing them so you get integer division…Casting this integer then to a
doubleis too late…Try: