Formatting Values with st.number_input

I’d like to format values in my st.number_input with the format arg. My step is 0.5, and I would like every number to have only one trailing 0, like such

1.0, 1.5, 2.0 …

Here is what I have tried:

st.number_input("Prop Line", value=5.5, step=0.5, format="%g")

This results in the following:

1, 1.5, 2, 2.5 … (no trailing 0 for the whole numbers)

I’ve found a way to give one trailing 0 to any number:

"{0:.1f}".format(0)
0.0

But streamlit doesn’t like "{0:.1f}" as a parameter for format:

st.number_input("Prop Line", value=5.5, step=0.5, format="0:.1f")

StreamlitAPIException: Format string for st.number_input contains invalid characters: 0:.1f

According to the documentation use the printf syle.

I have tried that, as my question states. It takes '%g' which adds a trailing 0 to only floats and doesn’t take '{0:.1f}' which would add a trailing 0 to ints as well.

Try this:

st.number_input("Prop Line", value=5.5, step=0.5, format="%0.1f")

printf guide

3 Likes

Solved. Thanks!

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