java - Calculation of BigDecimal -
i'm having problem getting calculation down. can addition, subtract, etc... i'm trying ((a - b)/b) in jasper reports.
here's i've tried far
${budget}.subtract($f{actual})/$f{budget}
but when compile it. says have expression problem. i'm using jasper studio expression editor
both $f{budget} , $f{actual} text fields , class java.math.bigdecimal
here's stace trace
net.sf.jasperreports.engine.jrexception: net.sf.jasperreports.engine.fill.jrexpressionevalexception: error evaluating expression : source text : $f{budgeted}.subtract($f{actual}.divide($f{budgeted})) @ com.jaspersoft.studio.editor.preview.view.control.reportcontroler.fillreport(reportcontroler.java:475) @ com.jaspersoft.studio.editor.preview.view.control.reportcontroler.access$18(reportcontroler.java:450) @ com.jaspersoft.studio.editor.preview.view.control.reportcontroler$4.run(reportcontroler.java:337) @ org.eclipse.core.internal.jobs.worker.run(worker.java:53) caused by: net.sf.jasperreports.engine.fill.jrexpressionevalexception: error evaluating expression : source text : $f{budgeted}.subtract($f{actual}.divide($f{budgeted})) @ net.sf.jasperreports.engine.fill.jrevaluator.evaluateestimated(jrevaluator.java:308) @ net.sf.jasperreports.engine.fill.jrcalculator.evaluateestimated(jrcalculator.java:582) @ net.sf.jasperreports.engine.fill.jrcalculator.estimatevariables(jrcalculator.java:181) @ net.sf.jasperreports.engine.fill.jrfilldataset.next(jrfilldataset.java:1261) @ net.sf.jasperreports.engine.fill.jrfilldataset.next(jrfilldataset.java:1235) @ net.sf.jasperreports.engine.fill.jrbasefiller.next(jrbasefiller.java:1588) @ net.sf.jasperreports.engine.fill.jrverticalfiller.fillreport(jrverticalfiller.java:149) @ net.sf.jasperreports.engine.fill.jrbasefiller.fill(jrbasefiller.java:939) @ net.sf.jasperreports.engine.fill.basefillhandle$reportfiller.run(basefillhandle.java:120) @ java.lang.thread.run(unknown source) caused by: java.lang.arithmeticexception: division 0 @ java.math.bigdecimal.divide(unknown source) @ projectsummary_1398114778002_614232.evaluateestimated(projectsummary_1398114778002_614232:405) @ net.sf.jasperreports.engine.fill.jrevaluator.evaluateestimated(jrevaluator.java:295) ... 9 more
${budget}.subtract($f{actual})
return bigdecimal
reference, cannot use division symbol /
because supported primitives (and class wrappers when unboxed). so, should use bigdecimal#divide
instead.
${budget}.subtract($f{actual}).divide($f{budget})
from question edit:
caused by: java.lang.arithmeticexception: division zero
your $f{budget}
variable should not have 0 value. use ternary operator solve problem:
${budget}.subtract($f{actual}).divide( ($f{budget}.compareto(bigdecimal.zero) == 0 ? bigdecimal.one : $f{budget}) )
from new comment:
i need if $f{budget} 0 calculation field gets assigned 0 , number other wise.
just modify usage of ternary operator:
($f{budget} == null || $f{budget}.compareto(bigdecimal.zero) == 0)) ? bigdecimal.zero : $f{budget}.subtract($f{actual}).divide($f{budget})
Comments
Post a Comment