Working with numbers

Rounding

If you're working with ask values, often you'll want to round your result to a whole number. You can use the round filter for this.

For example, this will round the user's highest previous contribution

{{ hpc_raw | round }}

If you'd like to always round up (ceil), or always round down (floor):

{{ hpc_raw }}  //let's say this is 42.7
{{ hpc_raw | round(1, 'floor') }} // 42
{{ hpc_raw | round(1, 'ceil') }} // 43

[block:api-header]
{
  "type": "basic",
  "title": "Arithmetic"
}
[/block]

Twig is a loosely typed language. This means that it can convert strings that look like numbers into numbers for the purposes of manipulation. 

{{ "3" +4 }} // prints 7


This is important because all values returned by Twig will be treated as strings. 

Twig comes with all of the standard mathematical operators:

Quoting from the Twig documentation:

- `+`: Adds two objects together (the operands are casted to numbers). `{{ 1 + 1 }}` is 2.
- `-` : Subtracts the second number from the first one. `{{ 3 - 2 }}` is 1.
- `/`: Divides two numbers. The returned value will be a floating point number. `{{ 1 / 2 }}` is 0.5.
- `%`: Calculates the remainder of an integer division. `{{ 11 % 7 }}` is 4.
- `//`: Divides two numbers and returns the floored integer result. `{{ 20 // 7 }}` is 2, `{{ -20 // 7 }}` is -3 (this is just syntactic sugar for the round filter).
- `*`: Multiplies the left operand with the right one. `{{ 2 * 2 }} `would return 4.
- `**`: Raises the left operand to the power of the right operand. `{{ 2 ** 3 }}` would return 8.