Summing strings

Hi. I have fields like

200.000

10.000

5.000

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 6.1.0.final using JasperReports Library version 6.1.0  -->
<!-- 2015-08-04T05:40:04 -->
<jasperReport
xmlns="http://jasperreports.sourceforge.net/jasperreports"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports
http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="test"
pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20"
rightMargin="20" topMargin="20" bottomMargin="20"
uuid="03c6ea1c-5c81-4a19-8d94-111f20cd415b">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="sampleDatabase"/>
    <queryString>
        <![CDATA[select * from test.test]]>
    </queryString>
    <field name="col1" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="col2" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="col3" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <variable name="Variable_1" class="java.lang.Double">
        <initialValueExpression><![CDATA[Double.parseDouble ($F{col1})]]></initialValueExpression>
    </variable>
    <variable name="Variable_2" class="java.lang.Double">
        <initialValueExpression><![CDATA[Double.parseDouble ($F{col2})]]></initialValueExpression>
    </variable>
    <variable name="Variable_3" class="java.lang.Double">
        <initialValueExpression><![CDATA[Double.parseDouble ($F{col3})]]></initialValueExpression>
    </variable>
    <variable name="total" class="java.lang.Double">
 
      <initialValueExpression><![CDATA[$V{Variable_1} +
$V{Variable_2} + $V{Variable_3}]]></initialValueExpression>
    </variable>
    <title>
        <band height="79" splitType="Stretch">
            <textField pattern="#,##0.000">
                <reportElement x="240" y="20" width="100" height="30" uuid="ff21d30b-5d5a-4e20-8d5d-1d66da270d5c"/>
                <textFieldExpression><![CDATA[$V{total}]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>


hozawa's picture
190427
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
274
Joined: Jul 3 2015 - 2:58am
Last seen: 1 month 1 week ago
Feedback
randomness