I know the OP says 'direct' but after a good hour wasted on this I personally came to this conclusion: 'direct' solutions for printing numbers in Jinja2 are either:
- hard to read (if you don't code in Python every day all day);
- or do stuff you may not necessarily want (e.g. drop trailing zeros.)
Ended up with this which is short and gives me code that's easy to read:
@app.template_filter()
def two_decimals_rounded_up(value):
"""
Filter to always print with two decimals, rounded up.
We need this because:
- The built-in `round` filter provided by Jinja2 will drop trailing zeros.
e.g. `|round(precision=2, method='ceil')`
- String formatting in Python is fucking unreadable.
On rounding up: https://stackoverflow.com/a/9232310/1717535
"""
return '{0:.2f}'.format(math.ceil(value * 100.0) / 100.0)
And then in the template:
{{ duration|two_decimals_rounded_up }}