How to convert a numeric string to a floating-point number or an integer in Anqi CMS template?
float
The filter can convert a string representing a number to a floating-point number. If the conversion fails, it returns0.0
.
integer
The filter can convert a string representing a number to an integer. If the conversion fails, it returns0
.
Usage Instructions
float
How to use the filter:
{{ obj|float }}
integer
How to use the filter:
{{ obj|integer }}
Here is an example demonstration
float
Filter
{{ "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
integer
Filter
{{ "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