Number Input Issues

Hi

Is there a way to:

  1. Only accept integers, or only floats up to n number of decimals through a number_input widget? (In my use case, I am not using the value parameter, and I may have any combination of the min / max parameter)
  2. remove the onscreen error message “Warning: NumberInput value below has type float, but format %i displays as integer.” (The previous forum suggestions don’t work)

Thanks in advance.

Cheers

Specifying the limits with integers will result in number_input only accepting integers.

number = st.number_input("Integer", 0, 10, "min", 1)

Using floats for the number_input limits allow entering a float. It will be up to the format kwarg to determine how many decimal places are displayed and up to your post-process to truncate it to that certain amount of decimal places.

number = st.number_input("Float", 0.0, 10.0, "min", 1.0, format="%.4f")
number = round(number, 4)

To show a Float without decimals, you can use "%.0f" instead of "%i". However, it will behave differently. "%.0f" rounds the float to the nearest integer whereas "%i" floors the number.

Thanks @edsaac, I knew that min/max would help to cast the number input. However, in my use case, I don’t know which combination of inputs I will receive from the user for this widget. So, my (common) code starts with None for these parameters, and the db field that the result gets outputted to, will need integers / floats (with a specific decimal count).

Yes, the result can be easily cast into integers / floats (in the subsequent code / widget callback).

Thanks for your help :slight_smile:

Cheers

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.