I am using Flask/Jinja2 template to show a number using |float filter.
Here is my code
{% set proc_err = nb_err|length / sum * 100 %}
({{proc_err|float}}%)
Output is a bit awkward:
17/189 (8.99470899471%)
I am looking for a way to make the places after dot limited to a number e.g. 2.
Desired output:
17/189 (8.99%)
Here is one approach ::
Tweak it as you like :)
It turns to be quite simple:
My code:
Can be changed a bit with:
or using format:
Reference can be found here on jinja2 github issue 70
(I'm assuming you use to Ionic v2)
Use this front-end code:
and put the following function into the .ts file:
You can use
round
to format afloat
to a given precision.Extracted from the docs:
round(value, precision=0, method='common')
Round the number to a given precision. The first parameter specifies the precision (default is
0
), the second the rounding method:common
rounds either up or downceil
always rounds upfloor
always rounds downIf you don’t specify a method
common
is used.Note that even if rounded to
0
precision, afloat
is returned. If you need a real integer, pipe it throughint
: