Multiplication in Django view
I am trying to represent decimal values as percentages i开发者_JS百科n a template. Specifically, I have several fields in a model with values such as '0.4000000059604645'. In models.py I have it representated as
information_percent = models.FloatField(blank=True)
And in a view I have:
org = Organizationaldata.objects.get(name=organization)
information_percent = org.information_percent
What I cannot figure out is how to get this to show as, for instance, '40.0' in the template. I have done a lot of searching and I can't figure out how to multiply the value by 100, or some other technique that will deliver the intended result.
And suggestions would be greatly appreciated.
Sounds like you need the built-in floatformat template filter.
For example:
views.py
org = Organizationaldata.objects.get(name=organization)
information_percent = 100 * org.information_percent
template.html
<span>Info percent: {{ information_percent|floatformat:1 }}</span>
Maybe this is what you want?
information_percent = int(org.information_percent*100)
If you want the fractional part you can extend it to:
information_percent = int(org.information_percent*100) + (org.information_percent*100 - int(org.information_percent*100))
精彩评论