I guess this is a very basic issue but
I do not understand the st.number_input rounding convention when using the format option. It seems to be neither the usual rounding half to even strategy nor a round up if halfway convention Here is a minimal example :
t = st.number_input(
r'$t$',
value=st.session_state.get('t', 0.1),
min_value=0.0,
max_value=0.3,
format="%0.2f",
step=0.01,
help='Thrust deduction factor, resolution=0.01',
placeholder="Type a number..."
)
st.write('Value entered : ', t)
st.write('round Value entered : ', round(t, 2))
t = round_up_halfway(t, 2)
st.write('Value retained : ', t)
In the first example, 0.125 is entered and displayed value is 0.13 (0.12 for round function)
In the second example, 0.245 is entered and displayed value is 0.24 (0.24 for round function)
The display format does not impact the underlying Python value.
If the widget holds the value .124 and is restricted to display two digits, it will appear on the front end as .12 (but still return .124). If the widget holds the value of .125 and is is restricted to display two digits, it will appear on the front end as .13 (but still return .125).
Thank you mathcatsand for you reply.
I understand the format option is just about the display but my issue is about how the rounding is performed (please consider exemple provided).
0.125 is displayed as 0,13 BUT
0.245 is displayed as 0,24
Can you explain the used rounding convention ?
Regards,
Jonas
By “under the hood” I mean what the computers are actually doing. We, as humans, represent numbers in base 10. Computers work in binary. We input something in base 10, the computer converts it to binary to store it and work with it, then it gets converted back to base 10 to show us. If we aren’t working with integers, that conversion to binary and back can introduce rounding errors.
I am doubtful that it would be prioritized since it’s a limitation of working with floats in programming. You could file a feature request on GitHub if you want, ut if it’s really important to you, your best bet is probably inserting your own logic to work with intergers instead of floats or somehow introduce your own margin of error to force the values to meet whatever threshold you want.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.