Remove trailing 0 when displaying data in input number of ModelForm


I can not figure our how to apply something similar to {{object.target_budget|floatformat:"-2"}} when editing it in a form. I want to allow the user to input and the database to save many decimals but I do not want to show it unless needed.

This is the field in my model, and when I use model form or inline formset, it shows for example: 5000.000000, and I want to remove the trailing 0s but not limit the database or user input.

    target_budget = models.DecimalField(
        max_digits=18, decimal_places=6, null=True, blank=True)

I’m not sure I understand what you’re asking here. Are you looking to create a custom filter and aren’t sure how to do that? Or are you looking for alternatives to creating a custom filter?

If the latter, you can create a function in the model that can be used as an attribute to be rendered.

I will try to illustrate with the attached screen shot, the number of trailing zeros make it a bit hard to read. I want to allow many digits in the db but not show more than needed when editing info in a field.


Ok, so it sounds like you’re looking to change this in the input field.

There are two aspects to this. First, you want the initial display to be limited to two decimal positions - you can do this by filtering the field at the time it’s being prepared in the form.

The other aspect is handling that field while it’s being edited in the browser. If you’re looking to restrict data entry to 2 decimal places before the data is submitted, you’ll need to add some sort of JavaScript handler to manage what the user can do in the browser.


Not really, I want it to show with as few decimals as possible, but not limit user input or database storage.

If the database has stores 1.4253 and I show it in a form as 1.43, it will overwrite the database.

I want it to show the realdata in the database, but for readability not show any trailing zeros.
I want the form to show
5000.000000 as 5000
5000.100000 as 5000.1
5000.010000 as 5000.01
5000.001000 as 5000.001