Convert a number string to a floating-point number or integer

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

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

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

Usage Instructions

floatHow to use the filter:

{{ obj|float }}

integerHow to use the filter:

{{ obj|integer }}

Here is an 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 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 }}

Display results

0
0
5
5
5
6
-100