# Simple Math with String to Int

**URL:** <https://forum.ansible.com/t/simple-math-with-string-to-int/32370>\
**Category:** Ansible Project\
**Created:** [March 3, 2020, 2:14am UTC](https://forum.ansible.com/t/simple-math-with-string-to-int/32370 "2020-03-03T02:14:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![M\_F](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@M\_F](https://forum.ansible.com/u/M_F)\
**Post date:** [March 3, 2020, 2:14am UTC](https://forum.ansible.com/t/simple-math-with-string-to-int/32370/1 "2020-03-03T02:14:41Z")

</div>

Hi All,

Im having trouble with setting a fact, doing some math, and having the result be an INT

To start, i set the base Var

set\_fact: SOMENUMBER = 8

Ansible stores this value as a String

Later, i need to divide that number by 2 and get the result as an INT

set\_fact: myvar = “{{ SOMENUMBER / 2) }}”

Even then though, Ansible stores it as a string.

If I do this, i get an INT, but its a FLOAT 4.0 instead of 4

set\_fact: myvar = “{{ ((SOMENUMBER|int) / 2) }}”

I am having to resort to doing the following to get my answer of 4

set\_fact: myvar = “{{ ((SOMENUMBER|int) / 2)|replace(‘.0’,‘’) }}”

Is there a better way to take a single integer stored as a string, and perform math and output an INT more simply?

TIA!

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [March 3, 2020, 6:07am UTC](https://forum.ansible.com/t/simple-math-with-string-to-int/32370/2 "2020-03-03T06:07:48Z")

</div>

I'm afraid there isn't any. Jinja is a template engine, hence the default  
output is a string. This was the intention of the developers, I think.  
Otherwise one would have to explicitly convert the output of mathematics  
expressions to strings.

Some notes to the examples:

\* The output of the example isn't INT. It's a string. The consequent task  
&nbsp;&nbsp;confirms this

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: Variable myvar is a string  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: myvar is string

\* Also the following task will fail

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ myvar + 4 }}"

&nbsp;&nbsp;with error message:

&nbsp;&nbsp;&nbsp;&nbsp;"Unexpected templating type error occurred on ({{ myvar + 4 }}):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;coercing to Unicode: need string or buffer, int found"

\* Instead the conversion to integer works as expected

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ myvar|int + 4 }}"

&nbsp;&nbsp;gives:

&nbsp;&nbsp;&nbsp;&nbsp;"msg": "8"

HTH  
&nbsp;&nbsp;&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Martin\_Krizek](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@Martin\_Krizek](https://forum.ansible.com/u/Martin_Krizek)\
**Post date:** [March 3, 2020, 7:07am UTC](https://forum.ansible.com/t/simple-math-with-string-to-int/32370/3 "2020-03-03T07:07:24Z")

</div>

Jinja2 native types introduced in Ansible 2.7 should be what you want:  
[https://docs.ansible.com/ansible/latest/reference\_appendices/config.html#default-jinja2-native](https://docs.ansible.com/ansible/latest/reference_appendices/config.html#default-jinja2-native)

Martin

---

<div class="post-metadata">

**Author:** ![M\_F](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@M\_F](https://forum.ansible.com/u/M_F)\
**Post date:** [March 3, 2020, 8:16pm UTC](https://forum.ansible.com/t/simple-math-with-string-to-int/32370/4 "2020-03-03T20:16:06Z")

</div>

Thank You

I will check that out

Much appreciated
