Num_decimals not hard forcing the amount of decimals in a NumberField


When using a NumberField, one of the arguments that can be given is num_decimals which, according to the documentation “Specifies the number of decimals”. In one of my apps I am using a table in which the columns are NumberFields. I have now gotten the request to make sure that the number of decimals is the same for each row in a given column. E.g. if I want to set a soil’s settlement parameter (which are given with 4 decimals in the Eurocode), I want the settlement parameter for all soils in my table to have 4 decimals.

However, when entering a number like 0.1150 in a NumberField with num_decimals = 4, the number is presented as 0.115 in the table. Because of this the table is less easy to read since the decimal point is not always in the same position and a sense of accuracy is lost since a significant digit is lost (0.115 can be any number between 0.1145 and 1.1154 rounded to 3 decimals).

The num_decimals will work if a number with more decimals then the specified num_decimals is entered (e.g. if 1.1234 is entered in a field with num_decimals = 3, 1.123 will be shown in the field).

I am not sure if the behaviour I highlighted above is the intended behaviour. If yes, this post can be seen as a feature request to ask the num_decimals to hard force the amount of decimals in a field. It not, this post can be seen as a bug report regarding the functionality of num_decimals.

Thanks for your time!

Hi Rutger,

Thanks for your post and the elaborate explanation.
I can imagine that in large tables this would cause some wonky visuals.

Indeed, as you said this behavior currently is as expected so I will change this post to the Feature Requests category.

In other cases than yours one would prefer the trailing 0’s to be removed.
What would you suggest in terms of functionality to provide the option whether or not to include the trailing 0 in the visual?

I’ll obviously also add your use case to our internal issue tracker.


Hi Daniël,

Thanks for your reply.

I think there are a few possibilities to remove trailing zeros. Of course, changing the num_decimals to work the way I suggested requires an extra step on the side of the developer to remove trailing zeros if desired but I think the added flexibility of having the option of having them there makes this worth it.

Option 1: The developer themselves removes trailing zeros. This can easily be achieved by using formatting python - Formatting floats without trailing zeros - Stack Overflow

This formatting requires to convert the float to a string but I think a simple function can be created to keep the float as float.

def remove_trailing_zeroes(num: float, return_type: str | float = float) -> str | float:
    number_without_trailing_zeroes = f"{num:g}"
    return return_type(number_without_trailing_zeroes)

Option 2: Add a keyword argument to the NumberField class. Something like keep_trailing_zeroes: bool where the default value is False. This option would personally not have my preference since more arguments make the field more complex to use for new users.


1 Like

Awesome, thanks for the input!
We are going to take this into account and I’ll keep you updated on the progress in this topic.

1 Like