i have a variable which fetches the data from database
{{i.rewardpoints}}
and the values it returns such as 1.799 or 12 the db has multiple values which contains decimals and without decimals
but i need to show the values without decimals
how can i do this
To round to nearest integer:
{{ i.rewardpoints|floatformat:"0" }}
To get the integer part:
{{ i.rewardpoints|stringformat:"d" }}
The floatformat filter documentation
The stringformat filter documentation
In [19]: tpl = Template('{{ x|stringformat:"d" }} {{ x|floatformat:"0" }}')
In [20]: tpl.render(Context({'x': 1.1}))
Out[20]: u'1 1'
In [21]: tpl.render(Context({'x': 1.9}))
Out[21]: u'1 2'
Related
I am sending the following datetime timezone aware object to django template
current_time = timezone.now()
In my template i am showing it using
{{ current_time }}
it displays as:
Feb. 10, 2021, 3:25 a.m
I want it to display as
Feb. 10, 2021, 3:25 a.m UTC
I dont see which timezone the time is. So i want to be more clear by adding the timezone.
how can i do this in django template
try this
{{ current_time |date:"b d Y f a T" }}
b:Month, textual, 3 letters, lowercase. 'jan'
d:Day of the month, 2 digits with leading zeros. '01' to '31' Y:Year,
4 digits. '1999'
f:Time, in 12-hour hours and minutes, with minutes left off if
they’re zero. Proprietary extension.
a:'a.m.' or 'p.m.' (Note that this is slightly different than PHP’s
output, because this includes periods to match Associated Press
style.) 'a.m.'
T:Time zone of this machine. 'EST', 'MDT'
you can see more about date template on here:https://docs.djangoproject.com/en/3.1/ref/templates/builtins/#date
I have the definition of a variable, it's name and an associated comment in a YAML file and am trying to use Jinja2 to create an appropriate target file; in this case a proprietary config file
...
- comment: >
This is a comment which will almost certainly end up longer than standard eighty characters or at least on the occasion on which it does.
name: my_useful_variable
value: /a/long/example/path/to/my/file.txt
I would like this text to be rendered as follows:
# This is a comment which will almost certainly end up
# longer than standard eighty characters or at least on
# the occasion on which it does.
my_useful_variable = "/a/long/example/path/to/my/file.txt"
Does Jinja2 have any way of wrapping text so that the long comment line is limited in length and split over however many lines is necessary?
So far I have:
# {{item.comment}}
{{item.name}} = "{{item.value}}"
But this of course does not deal with the length of the comment.
Solution
Following on from the answer provided by #blhsing below, I came up with the following macro, which works fine for basic variables and simple lists (i.e. not dictionaries or more complex hierarchical data structures:
{% macro set_params(param_list, ind=4, wid=80) -%}
{% for item in param_list %}
{% if item.comment is defined %}{{item.comment|wordwrap(wid - ind - 2)|replace('', ' ' * ind +'# ', 1)|replace('\n', '\n' + ' ' * ind + '# ')}}
{% endif %}
{% if item.value is iterable and item.value is not string %}{{item.name|indent(ind, True)}} = [ {% for item_2 in item.value %}{{item_2}}{{ ", " if not loop.last else " " }}{% endfor %}{% else %}{{item.name|indent(ind, True)}} = {{item.value}}{% endif %}
{% endfor %}
{%- endmacro %}
To use this, simply pass a list of items similar to the spec given at the top together with the indentation and the page width.
A bit of explanation:
Line 3, If comment is defined then it is word wrapped to the correct length bearing in mind the width and the indent. The first replace deals with indenting the first line and the second indents subsequent lines. All prefixed with '# '
Line 5, depending on whether the variable is simple or iterable, it is rendered in the form name = value or name = [ value1, value2, value3 ]
Of course, it is not fool-proof but it meets my basic requirements.
You can prepend the given string with a newline character, then use the wordwrap filter to wrap the text into multiple lines first, and use the replace filter to replace newline characters with newline plus '# ':
{{ ('\n' ~ item.comment) | wordwrap(78) | replace('\n', '\n# ') }}
The above assumes you want each line to be no more than 80 characters. Change 78 to your desired line width minus 2 to leave room for '# '.
If you're doing this in Ansible, another option is to use the Ansible comment filter:
{{ item.comment | wordwrap(78) | comment }}
or, for more detailed control
{{ item.comment | wordwrap(78) | comment(decoration="# ", prefix="", postfix="") }}
Docs: https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#adding-comments-to-files
I'm having trouble formatting a list of values in Jinja.
Current List:
[0, 0.2608695652173913, 0]
Needed List:
[0, 26.08, 0]
Code:
[{{ record['result']|map(attribute='record')|join(', ') }}]
What is the correct syntax to apply the format filter with something like {0:0.2f}?
You can do something like this...
def FormatDecimal(value):
return "{0:0.2f}".format(float(value))
jinja2.filters.FILTERS['FormatDecimal'] = FormatDecimal
Then use this in your template...
{{ SomeValue | FormatDecimal }}
Hope this helps!
I have a listing counter "2370" and the page only shows "12" item so I want to calculate the pages number, I did the solution below
{% widthratio listing.total_count 12 1 %}
so, how I can save the result into a variable?
{% set total_pages = widthratio listing.total_count 12 1 %}
this one didn't work
If would choose Rohans comment, but if you would write your own templatetags, use an assignment_tag (https://docs.djangoproject.com/en/1.7/howto/custom-template-tags/#assignment-tags, exists since Django 1.4).
#register.assignment_tag
def page_numbers(total_items, items_per_page):
if not total_items:
return 0
# int divisions round down (1 / 12 = 0), so just add 1 to cover this
return total_items / items_per_page + 1
Within the template you should use;
{% page_numbers listing.total_count 12 as page_nrs %}
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%)
It turns to be quite simple:
My code:
{% set proc_err = nb_err|length / sum * 100 %}
({{proc_err|float}}%)
Can be changed a bit with:
{% set proc_err = nb_err|length / sum * 100 %}
({{'%0.2f' % proc_err|float}}%)
or using format:
({{'%0.2f'| format(proc_err|float)}}%)
Reference can be found here on jinja2 github issue 70
You can use round to format a float 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 down
ceil always rounds up
floor always rounds down
If you don’t specify a method common is used.
{{ 42.55|round }}
-> 43.0
{{ 42.55|round(1, 'floor') }}
-> 42.5
Note that even if rounded to 0 precision, a float is returned. If you need a real integer, pipe it through int:
{{ 42.55|round|int }}
-> 43
Here is one approach ::
{{ "%.2f"|format(result) }}
Tweak it as you like :)
(I'm assuming you use to Ionic v2)
Use this front-end code:
<div *ngIf="device.usage > 0">
{{ Round(device.usage)}} min
</div>
and put the following function into the .ts file:
Round(number):number
{
return parseFloat(Number((number/1000) / 60).toFixed(2));
}