djangodjango-templates

Django templates: Convert float to integer if it ends with .0?


In a Django template, is there a way to convert a float into an integer if and only if it ends in .0?

I have a field for population that is a float in my database. Sometimes it is 'really' a float, ending .5, but more often it ends .0, because there is a whole number of people, and in those cases I'd rather just show an integer.

{{ place.population }} people

Any ideas for a clever way to get round this in Django?


Solution

  • You can use the floatformat filter with a negative argument!