I would like something similar to the string formatting from the standard library.
我想要类似于标准库中的字符串格式。
'%' Percentage. Multiplies the number by 100 and displays in fixed ('f') format, followed by a percent sign.
'%'百分比。将数字乘以100并以固定('f')格式显示,后跟百分号。
6 个解决方案
#1
3
The newness of string.Formatter()
means that Django is not likely to support it built-in. Either write or find a template tag or filter that implements it.
string.Formatter()的新颖性意味着Django不太可能支持它内置。编写或查找实现它的模板标记或过滤器。
#2
45
I was looking for almost the same question and found the widthratio template tag. Instead of having the percentage already calculated like in your question, you can use this tag to calculate the percentage in the template from the original value and total value against which your percentage is calculated. It does the job if you only need an integer percentage without precision:
我正在寻找几乎相同的问题,并找到了widthratio模板标签。您可以使用此标记从原始值和计算百分比的总值计算模板中的百分比,而不是像您的问题中那样计算百分比。如果您只需要一个没有精度的整数百分比,它就可以完成工作:
{% widthratio value total_value 100 %}
Ref: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#widthratio
参考:https://docs.djangoproject.com/en/dev/ref/templates/builtins/#widthratio
#3
21
In case somebody is looking for the answser, this is how I solved the problem with a custom templatetag:
如果有人正在寻找答案,这就是我用自定义模板标签解决问题的方法:
from django import template
register = template.Library()
@register.filter
def percentage(value):
return format(value, "%")
#4
13
Here's what I'm using (we only show decimals, not floats, by the way):
这是我正在使用的(顺便说一下,我们只显示小数,而不是浮点数):
@register.filter
def as_percentage_of(part, whole):
try:
return "%d%%" % (float(part) / whole * 100)
except (ValueError, ZeroDivisionError):
return ""
Use it like this:
像这样用它:
Monkeys constitute {{ monkeys|as_percentage_of:animals }} of all animals.
where if monkeys is 3 and animals is 6, you'll get:
如果猴子是3,动物是6,你会得到:
50%
#5
7
This is how I solved the problem:
这就是我解决问题的方法:
from django import template
register = template.Library()
def percentage(value):
return '{0:.2%}'.format(value)
register.filter('percentage', percentage)
#6
1
Better solution with internationalization works with python 2.5.
更好的国际化解决方案适用于python 2.5。
from django import template
register = template.Library()
from django.template.defaultfilters import floatformat
@register.filter
def percent(value):
if value is None:
return None
return floatformat(value * 100.0, 2) + '%'
#1
3
The newness of string.Formatter()
means that Django is not likely to support it built-in. Either write or find a template tag or filter that implements it.
string.Formatter()的新颖性意味着Django不太可能支持它内置。编写或查找实现它的模板标记或过滤器。
#2
45
I was looking for almost the same question and found the widthratio template tag. Instead of having the percentage already calculated like in your question, you can use this tag to calculate the percentage in the template from the original value and total value against which your percentage is calculated. It does the job if you only need an integer percentage without precision:
我正在寻找几乎相同的问题,并找到了widthratio模板标签。您可以使用此标记从原始值和计算百分比的总值计算模板中的百分比,而不是像您的问题中那样计算百分比。如果您只需要一个没有精度的整数百分比,它就可以完成工作:
{% widthratio value total_value 100 %}
Ref: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#widthratio
参考:https://docs.djangoproject.com/en/dev/ref/templates/builtins/#widthratio
#3
21
In case somebody is looking for the answser, this is how I solved the problem with a custom templatetag:
如果有人正在寻找答案,这就是我用自定义模板标签解决问题的方法:
from django import template
register = template.Library()
@register.filter
def percentage(value):
return format(value, "%")
#4
13
Here's what I'm using (we only show decimals, not floats, by the way):
这是我正在使用的(顺便说一下,我们只显示小数,而不是浮点数):
@register.filter
def as_percentage_of(part, whole):
try:
return "%d%%" % (float(part) / whole * 100)
except (ValueError, ZeroDivisionError):
return ""
Use it like this:
像这样用它:
Monkeys constitute {{ monkeys|as_percentage_of:animals }} of all animals.
where if monkeys is 3 and animals is 6, you'll get:
如果猴子是3,动物是6,你会得到:
50%
#5
7
This is how I solved the problem:
这就是我解决问题的方法:
from django import template
register = template.Library()
def percentage(value):
return '{0:.2%}'.format(value)
register.filter('percentage', percentage)
#6
1
Better solution with internationalization works with python 2.5.
更好的国际化解决方案适用于python 2.5。
from django import template
register = template.Library()
from django.template.defaultfilters import floatformat
@register.filter
def percent(value):
if value is None:
return None
return floatformat(value * 100.0, 2) + '%'