Summing strings

Hi. I have fields like




they are in a string. I want to sum them that and print it in the right format, 215.000 in this case. 

I tried creating a variable with the expression Double.parseDouble ($F{field20}) but it didn't work


mnfaria1994's picture
Joined: Jul 1 2015 - 2:31am
Last seen: 8 years 1 month ago

2 Answers:

Seems to be working.


<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version using JasperReports Library version 6.1.0  -->
<!-- 2015-08-04T05:40:04 -->
xsi:schemaLocation="" name="test"
pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20"
rightMargin="20" topMargin="20" bottomMargin="20"
    <property name="" value="sampleDatabase"/>
        <![CDATA[select * from test.test]]>
    <field name="col1" class="java.lang.String">
    <field name="col2" class="java.lang.String">
    <field name="col3" class="java.lang.String">
    <variable name="Variable_1" class="java.lang.Double">
        <initialValueExpression><![CDATA[Double.parseDouble ($F{col1})]]></initialValueExpression>
    <variable name="Variable_2" class="java.lang.Double">
        <initialValueExpression><![CDATA[Double.parseDouble ($F{col2})]]></initialValueExpression>
    <variable name="Variable_3" class="java.lang.Double">
        <initialValueExpression><![CDATA[Double.parseDouble ($F{col3})]]></initialValueExpression>
    <variable name="total" class="java.lang.Double">
      <initialValueExpression><![CDATA[$V{Variable_1} +
$V{Variable_2} + $V{Variable_3}]]></initialValueExpression>
        <band height="79" splitType="Stretch">
            <textField pattern="#,##0.000">
                <reportElement x="240" y="20" width="100" height="30" uuid="ff21d30b-5d5a-4e20-8d5d-1d66da270d5c"/>

hozawa's picture
Joined: Apr 24 2010 - 4:31pm
Last seen: 4 years 3 months ago

You should define your field as Integer or Double and not as String, if it is a number! after that, its easy


define a new variable with calculation operation sum, increment type none and reset type report.

i think the expression should be your field ( $F{XYZ} ), the initial value expression should be 0

value class name should be java.lang.Float or java.lang.Integer, depends on your data


if your field will stay a String try the expression $F{XYZ}.replace(".", "")

grafro's picture
Joined: Jul 3 2015 - 2:58am
Last seen: 1 month 1 week ago