# Performing calculations in ODK Build

**URL:** <https://forum.getodk.org/t/performing-calculations-in-odk-build/12733>\
**Category:** Support\
**Created:** [April 17, 2018, 9:46pm UTC](https://forum.getodk.org/t/performing-calculations-in-odk-build/12733 "2018-04-17T21:46:25Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![coderscoven](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/coderscoven/32/3836_2.png) [@coderscoven](https://forum.getodk.org/u/coderscoven)\
**Post date:** [April 17, 2018, 9:46pm UTC](https://forum.getodk.org/t/performing-calculations-in-odk-build/12733/1 "2018-04-17T21:46:25Z")

</div>

Hi guys. Am having problems performing calculations using ODK Build. Like trying to sum up two numbers in numeric fields: txt\_one and txt\_two. Then displaying the results in numeric field, txt\_three.  
I have this in calculate box of txt\_three.  
${txt\_one}+${txt\_two}.  
And I keep on getting an error when uploading the form to aggregate.  
Am new to ODK. So any help is appreciated.

---

<div class="post-metadata">

**Author:** ![Grzesiek2010](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/grzesiek2010/32/2881_2.png) [@Grzesiek2010](https://forum.getodk.org/u/Grzesiek2010)\
**Post date:** [April 17, 2018, 10:12pm UTC](https://forum.getodk.org/t/performing-calculations-in-odk-build/12733/2 "2018-04-17T22:12:16Z")

</div>

In ODK Build it doesn't work in the same way as in xls.  
You need rater add something like:  
`/data/txt_one + /data/txt_two`

the path depends on your groups.

---

<div class="post-metadata">

**Author:** ![coderscoven](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/coderscoven/32/3836_2.png) [@coderscoven](https://forum.getodk.org/u/coderscoven)\
**Post date:** [April 18, 2018, 5:33am UTC](https://forum.getodk.org/t/performing-calculations-in-odk-build/12733/3 "2018-04-18T05:33:37Z")

</div>

Thanks for the response. Is data the name of the XML file?

---

<div class="post-metadata">

**Author:** ![Grzesiek2010](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/grzesiek2010/32/2881_2.png) [@Grzesiek2010](https://forum.getodk.org/u/Grzesiek2010)\
**Post date:** [April 18, 2018, 8:51am UTC](https://forum.getodk.org/t/performing-calculations-in-odk-build/12733/4 "2018-04-18T08:51:55Z")

</div>

No data is the name of the main section your question is inside. If you have another group, for example, you need to add it too.

In this case it would be `/data/group1/text`

```
<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jr="http://openrosa.org/javarosa">
  <h:head>
    <h:title>testForm</h:title>
    <model>
      <instance>
        <data id="build_testForm_1524041259">
          <meta>
            <instanceID/>
          </meta>
          <group1>
            <text/>
          </group1>
        </data>
      </instance>
      <itext>
        <translation lang="English">
          <text id="/data/group1:label">
            <value>group1</value>
          </text>
          <text id="/data/group1/text:label">
            <value>Text</value>
          </text>
        </translation>
      </itext>
      <bind nodeset="/data/meta/instanceID" type="string" readonly="true()" calculate="concat('uuid:', uuid())"/>
      <bind nodeset="/data/group1/text" type="string"/>
    </model>
  </h:head>
  <h:body>
    <group>
      <label ref="jr:itext('/data/group1:label')"/>
      <input ref="/data/group1/text">
        <label ref="jr:itext('/data/group1/text:label')"/>
      </input>
    </group>
  </h:body>
</h:html>

```

---

<div class="post-metadata">

**Author:** ![coderscoven](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/coderscoven/32/3836_2.png) [@coderscoven](https://forum.getodk.org/u/coderscoven)\
**Post date:** [April 18, 2018, 9:30am UTC](https://forum.getodk.org/t/performing-calculations-in-odk-build/12733/5 "2018-04-18T09:30:12Z")

</div>

Okay got you explanation on that. Let me try it out and will get back to you. Thanks

---

<div class="post-metadata">

**Author:** ![coderscoven](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/coderscoven/32/3836_2.png) [@coderscoven](https://forum.getodk.org/u/coderscoven)\
**Post date:** [April 18, 2018, 10:04am UTC](https://forum.getodk.org/t/performing-calculations-in-odk-build/12733/6 "2018-04-18T10:04:12Z")

</div>

Alright here is my xml file. I keep on getting an error: "java.lang.boolean cannot be cast to java.lang.integer" when i run it on ODK collect.

```
<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jr="http://openrosa.org/javarosa">
  <h:head>
    <h:title>Test Form one</h:title>
    <model>
      <instance>
        <data id="build_Untitled-Form_1524045635">
          <meta>
            <instanceID/>
          </meta>
          <num_one/>
          <num_two/>
          <calculate/>
        </data>
      </instance>
      <itext>
        <translation lang="English">
          <text id="/data/num_one:label">
            <value>enter first number</value>
          </text>
          <text id="/data/num_two:label">
            <value>enter second number</value>
          </text>
          <text id="/data/calculate:label">
            <value>Answer</value>
          </text>
        </translation>
      </itext>
      <bind nodeset="/data/meta/instanceID" type="string" readonly="true()" calculate="concat('uuid:', uuid())"/>
      <bind nodeset="/data/num_one" type="int"/>
      <bind nodeset="/data/num_two" type="int"/>
      <bind nodeset="/data/calculate" type="int" calculate="calculate=&quot;/data/num_one + /data/num_two&quot;"/>
    </model>
  </h:head>
  <h:body>
    <input ref="/data/num_one">
      <label ref="jr:itext('/data/num_one:label')"/>
    </input>
    <input ref="/data/num_two">
      <label ref="jr:itext('/data/num_two:label')"/>
    </input>
    <input ref="/data/calculate">
      <label ref="jr:itext('/data/calculate:label')"/>
    </input>
  </h:body>
</h:html>

```

---

<div class="post-metadata">

**Author:** ![Grzesiek2010](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/grzesiek2010/32/2881_2.png) [@Grzesiek2010](https://forum.getodk.org/u/Grzesiek2010)\
**Post date:** [April 18, 2018, 10:09am UTC](https://forum.getodk.org/t/performing-calculations-in-odk-build/12733/7 "2018-04-18T10:09:25Z")

</div>

Could you attach a screen from the ODK Build (which includes your calculation)?

---

<div class="post-metadata">

**Author:** ![coderscoven](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/coderscoven/32/3836_2.png) [@coderscoven](https://forum.getodk.org/u/coderscoven)\
**Post date:** [April 18, 2018, 2:51pm UTC](https://forum.getodk.org/t/performing-calculations-in-odk-build/12733/8 "2018-04-18T14:51:22Z")

</div>

Am using the offline version of ODK build. Below is the screen shot.

 ![screen2](https://getodk.b-cdn.net/uploads/default/original/2X/1/19c90d64b9db05f02bc7adc60fb6200673c41ddb.png)

---

<div class="post-metadata">

**Author:** ![Grzesiek2010](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/grzesiek2010/32/2881_2.png) [@Grzesiek2010](https://forum.getodk.org/u/Grzesiek2010)\
**Post date:** [April 19, 2018, 6:03am UTC](https://forum.getodk.org/t/performing-calculations-in-odk-build/12733/9 "2018-04-19T06:03:58Z")

</div>

Please try just `/data/num_one + /data/num_two` in the `calculate` field.

---

<div class="post-metadata">

**Author:** ![coderscoven](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/coderscoven/32/3836_2.png) [@coderscoven](https://forum.getodk.org/u/coderscoven)\
**Post date:** [April 19, 2018, 10:22am UTC](https://forum.getodk.org/t/performing-calculations-in-odk-build/12733/10 "2018-04-19T10:22:04Z")

</div>

Okay i finally got it to work! I replaced the expression in the calculate field with what you said. And in its Default Value field, i added this '/data/calculate' (without the quotes). Eureka!! worked like a charm. I have uploaded the xml file in case any one else runs in to the same problem.  
Thanks @Grzesiek2010  
[Test Form One.xml](https://getodk.b-cdn.net/uploads/default/original/2X/5/5cac220510cc0cbf1914f32f7a7a7595c0026a81.xml) (1.9 KB)

---

<div class="post-metadata">

**Author:** ![tjaramillo](https://getodk.b-cdn.net/letter_avatar_proxy/v4/letter/t/7c8e57/32.png) [@tjaramillo](https://forum.getodk.org/u/tjaramillo)\
**Post date:** [October 16, 2018, 5:38pm UTC](https://forum.getodk.org/t/performing-calculations-in-odk-build/12733/11 "2018-10-16T17:38:08Z")

</div>

Hi. Your advice for performing calculations in ODK build was really helpful. I wonder if you can help me verify if this constraint syntax in ODK build is correct. In this case Tested1st\_HIVpos should be less than or equal to Tested\_4HIV. Thank you,

 ![image](https://getodk.b-cdn.net/uploads/default/original/2X/7/79a1a3a5bfaf5487815c6a1f60764174362c479f.png)

---

<div class="post-metadata">

**Author:** ![Xiphware](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/xiphware/32/2587_2.png) [@Xiphware](https://forum.getodk.org/u/Xiphware)\
**Post date:** [October 16, 2018, 8:59pm UTC](https://forum.getodk.org/t/performing-calculations-in-odk-build/12733/12 "2018-10-16T20:59:19Z")

</div>

`constraint=". <= /data/HTS/Tested_4HIV"` looks OK, but I note that your hint says "Your answer must be between 0 and 1000 **or** less than Tested for HIV..." whereas you actual constraint specifies **and**... So you might want to check that.

---

<div class="post-metadata">

**Author:** ![tjaramillo](https://getodk.b-cdn.net/letter_avatar_proxy/v4/letter/t/7c8e57/32.png) [@tjaramillo](https://forum.getodk.org/u/tjaramillo)\
**Post date:** [October 17, 2018, 3:04pm UTC](https://forum.getodk.org/t/performing-calculations-in-odk-build/12733/13 "2018-10-17T15:04:42Z")

</div>

The constraint should read as follows: Constraint = (/data/HTS/Tested1st\_HIVpos) \<= (/data/HTS/Tested\_4HIV) in order to work as both fields are in a group named HTS.

 ![image](https://getodk.b-cdn.net/uploads/default/original/2X/3/33edf19a90fd5ddf769ab5824de06a22b4528dda.png)October 17, 2018
