Convert a numeric string to a float or integer

How to convert numeric string to float or integer in AnQi CMS template?

floatThe filter can convert a numeric string to a floating-point number. If the conversion fails, it returns0.0.

integerThe filter can convert a numeric string to an integer. If the conversion fails, it returns0.

Usage method

floatHow to use the filter:

{{ obj|float }}

integerHow to use the filter:

{{ obj|integer }}

Example Demonstration

floatFilter

{{ "foobar"|float }}
{{ nil|float }}
{{ "5.5"|float }}
{{ 5|float }}
{{ "5.6"|integer|float }}
{{ -100|float }}
{% if 5.5 == 5.500000 %}5.5 is 5.500000{% endif %}
{% if 5.5 != 5.500001 %}5.5 is not 5.500001{% endif %}

Display result

0.000000
0.000000
5.500000
5.000000
5.000000
-100.000000
5.5 is 5.500000
5.5 is not 5.500001

integerFilter

{{ "foobar"|integer }}
{{ nothing|integer }}
{{ "5.4"|float|integer }}
{{ "5.5"|float|integer }}
{{ "5.6"|float|integer }}
{{ 6|float|integer }}
{{ -100|integer }}

Display result

0
0
5
5
5
6
-100