Convert a numeric string to a floating point number or an integer

How to convert a numeric string to a floating-point number or an integer in the Anqi CMS template?

floatFilters can convert numeric strings into floating point numbers. If the conversion fails, return0.0.

integerFilters can convert numeric strings into integers. If the conversion fails, return0.

How to use

floatHow to use filters:

{{ obj|float }}

integerHow to use filters:

{{ obj|integer }}

Sample Demo

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 %}

Show results

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 }}

Show results

0
0
5
5
5
6
-100