Search code examples
scriban

How to convert double to integer in scriban


I am trying to use '*' operator in scriban to have a string being concatenated several times like this:

{{
    number = 4 / 2
    'text' * number
}}

However this gives me an exception: Operator * is not supported for the expression. Only working on string x int or int x string. However variable number obviously contains string. How to fix the template?


Solution

  • In scriban operator '/' always produces double, regardless of the outcome of operation. To have an integer you need to use '//' operator instead:

    {{
        number = 4 // 2
        'text' * number
    }}
    

    In fact operator '//' appears to be the only way of converting double to integer in scriban. If you have a ready value that only needs convertion you can always divide it with '//' by 1:

    {{
        number = number // 1
        'text' * number
    }}