Jump to content
We've recently updated our Privacy Statement, available here ×

How to perform add,subtract on 'Double' Fields


thelinuxmaniac

Recommended Posts

Hi,

I have three Double fields as:

 

Code:

<field name="grandtotal" class="java.lang.Double"/>
<field name="amount_paid" class="java.lang.Double"/>
<field name="previous_due" class="java.lang.Double"/>

 

And I want to calculate Balance using the formula:

BALANCE_DUE = grandtotal+previous_due-amount_paid

 

When I define a variable to calculate BALANCE_DUE as shown below

Code:
[code]
<variable name="BALANCE_DUE" class="java.lang.Double" resetType="Report" calculation="Nothing">
<variableExpression><![CDATA[sUM( $F{grandtotal} + $F{amount_paid})]]></variableExpression>
</variable>

 

I get the following error message

Code:
[code]
et.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file: 1. The operator + is undefined for the argument type(s) java.lang.Double, java.lang.Double value = (java.lang.Double)(SUM( ((java.lang.Double)field_grandtotal.getValue()) + ((java.lang.Double)field_amount_paid.getValue()))); <------------------------------------------------------------------------------------------------> 2. The operator + is undefined for the argument type(s) java.lang.Double, java.lang.Double value = (java.lang.Double)(SUM( ((java.lang.Double)field_grandtotal.getOldValue()) + ((java.lang.Double)field_amount_paid.getOldValue()))); <------------------------------------------------------------------------------------------------------> 3. The operator + is undefined for the argument type(s) java.lang.Double, java.lang.Double value = (java.lang.Double)(SUM( ((java.lang.Double)field_grandtotal.getValue()) + ((java.lang.Double)field_amount_paid.getValue()))); <------------------------------------------------------------------------------------------------> 3 errors

Why cannot + and - operators be applied to Double value (and only applied to strings) ? Please suggest any other possible method to calculate the BALANCE_DUE.

Link to comment
Share on other sites

  • Replies 4
  • Created
  • Last Reply

Top Posters In This Topic

It is related to the underlying functionality of the '+' operator in java. Strings are able to be appended using the '+' even though they are objects, its an exception to the rule.

 

To add the Double objects you have to treat each field as an object and use dot-notation in your expression fro the BALANCE_DUE field.

 

new Double($F{grandtotal}.doubleValue() + $F{previous_due}.doubleValue() - $F{amount_paid}.doubleValue())

 

Notice that there is no ";" at the end of the expression. Now if one of the above objects is null, a null ptr expression would occur. To avoid this you have to modify the expression as follows...

 

 

new Double(

$F{grandtotal}==null?0.0:$F{grandtotal}.doubleValue() +

$F{previous_due}==null?0.0:$F{previous_due}.doubleValue()

-

$F{amount_paid}==null?0.0:$F{amount_paid}.doubleValue())

 

Now there is a check to use 0.0 if the object is null. This depends on your data source etc. of course, if the object could be null.

 

hth,

 

Robin

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...