Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 275
AP257 Avatar asked Dec 30 '10 20:12

AP257


1 Answers

You can use the floatformat filter with a negative argument!

like image 119
Bernhard Vallant Avatar answered Sep 17 '22 11:09

Bernhard Vallant