[Fixed]-Django templates: Convert float to integer if it ends with .0?

40👍

You can use the floatformat filter with a negative argument!

2👍

You could write a template tag, but I’d recommend making this a method on your model.

class Country(models.Model):

    ...

    def get_population(self):
        if self.population == int(self.population):
            self.population = int(self.population)
        return self.population

Then in your template, instead of

{{ obj.population }}

You would use this:

{{ obj.get_population }}

Leave a comment