Convert integer to string Jinja

Question:

I have an integer

{% set curYear = 2013 %}

In {% if %} statement I have to compare it with some string. I can’t set curYear to string at the beginning because I have to decrement it in loop.

How can I convert it?

Asked By: Glen Swift

||

Answers:

I found the answer.

Cast integer to string:

myOldIntValue|string

Cast string to integer:

myOldStrValue|int
Answered By: Glen Swift

The OP needed to cast as string outside the {% set ... %}.
But if that not your case you can do:

{% set curYear = 2013 | string() %}

Note that you need the parenthesis on that jinja filter.

If you’re concatenating 2 variables, you can also use the ~ custom operator.

Answered By: louis_guitton

Formatting someInt as a string can be achieved through:

'{0:d}'.format(someInt)

This syntax come from ansible, that also uses Python and Jinja. behind the scene.

Answered By: 张馆长

How can we convert int means 123 to "One Thousand Two Hundred Three"

Answered By: Asif Patel Ext
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.