# How do I round a value in Jinja?

**URL:** <https://forum.ansible.com/t/how-do-i-round-a-value-in-jinja/16084>\
**Category:** Ansible Project\
**Created:** [August 30, 2014, 5:30pm UTC](https://forum.ansible.com/t/how-do-i-round-a-value-in-jinja/16084 "2014-08-30T17:30:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![oravirt](https://avatars.discourse-cdn.com/v4/letter/o/3e96dc/32.png) [@oravirt](https://forum.ansible.com/u/oravirt)\
**Post date:** [August 30, 2014, 5:30pm UTC](https://forum.ansible.com/t/how-do-i-round-a-value-in-jinja/16084/1 "2014-08-30T17:30:41Z")

</div>

Hi,

I’ve got a template which does a few calculations on memory (to set up hugepages etc), and I need to round the result but I’m not sure how to.

`  
This is the expression:

“{{ (((60/100)\* ansible\_memtotal\_mb)\*1024)/2048 }}”

`

I usually end up with a decimal, but I need to get rid of the decimal:

`  
Result: 600.6 and I’d like to round that to 601

Result: 1201.1 should be rounded to 1201  
`  
etc

Any idea on how to do that?

Thanks!  
/M

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [August 30, 2014, 5:41pm UTC](https://forum.ansible.com/t/how-do-i-round-a-value-in-jinja/16084/2 "2014-08-30T17:41:04Z")

</div>

You are looking for the aptly named “round” filter:

[http://jinja.pocoo.org/docs/dev/templates/#round](http://jinja.pocoo.org/docs/dev/templates/#round)

some\_val|round

---

<div class="post-metadata">

**Author:** ![oravirt](https://avatars.discourse-cdn.com/v4/letter/o/3e96dc/32.png) [@oravirt](https://forum.ansible.com/u/oravirt)\
**Post date:** [August 30, 2014, 6:00pm UTC](https://forum.ansible.com/t/how-do-i-round-a-value-in-jinja/16084/3 "2014-08-30T18:00:28Z")

</div>

Doh!

Awesome, that did the trick.

someval |round|int

Thanks!  
/M

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [September 3, 2014, 7:48am UTC](https://forum.ansible.com/t/how-do-i-round-a-value-in-jinja/16084/4 "2014-09-03T07:48:42Z")

</div>

Have a look at the '//' operator :  
[http://jinja.pocoo.org/docs/dev/templates/#math](http://jinja.pocoo.org/docs/dev/templates/#math)

- it rounds down, but saves the extra step.

---

<div class="post-metadata">

**Author:** ![Bohdan\_Sydor](https://avatars.discourse-cdn.com/v4/letter/b/f475e1/32.png) [@Bohdan\_Sydor](https://forum.ansible.com/u/Bohdan_Sydor)\
**Post date:** [September 3, 2014, 8:56am UTC](https://forum.ansible.com/t/how-do-i-round-a-value-in-jinja/16084/5 "2014-09-03T08:56:17Z")

</div>

Hi,

Please take a look at this example:

innodb\_buffer\_pool\_size = {{  
(0.85\*ansible\_memtotal\_mb/1024)|int }}G

As a result I get:  
innodb\_buffer\_pool\_size = 120G

---

<div class="post-metadata">

**Author:** ![oravirt](https://avatars.discourse-cdn.com/v4/letter/o/3e96dc/32.png) [@oravirt](https://forum.ansible.com/u/oravirt)\
**Post date:** [September 3, 2014, 11:37am UTC](https://forum.ansible.com/t/how-do-i-round-a-value-in-jinja/16084/6 "2014-09-03T11:37:05Z")

</div>

Hi,

Yeah it’s already sorted, but thanks!

/M
