i have the following code in a form with submit button:
var= st.number_input("Test Number", format="%.2f", value = temp_val, max_value=999999999999.00, min_value=0.00)
when i enter a large number and press “submit”, the value is either rounded up or rounded down. Is the form_submit button converting it to integer? When i check the sql database, it is also rounded so I assume the rounding off happens before the submit function.
Steps to reproduce
- Enter a large number (i used 2000502.99 and 2000509.99). For the 1st number, it rounds down and rounds up for the 2nd number. I need the exact values
- Submit form
Number is not rounded off and has 2 decimal places as float type in the database.
Larger inputs are rounded off when i check them in the database, and of course when i retrieve them.
- Streamlit version: using streamlit cloud v1.13.0
- OS version: Windows 10 64-bit
- Browser version: Opera GX LVL4 (core: 91.0.4516.72)
can’t link app because it’s private. data are private
Based on my observations, it rounds off after the 7th number:
If i enter 200000502.99 → rounds off to 200000000.00
If i enter 2000502.99 → rounds off to 2000500.00
If i enter 200502.99 → rounds off to 200503.00
so the smaller the value is the more precise it is. BUT i want it also precise for larger values.
Actually I don’t see the same behavior when I try number input.
Could you post a bit more of your code. Particularly what you do to “var” after the number input?
After several more tests, I’ve found out it’s not an issue with st.number_input but with the st.forms itself. If you test using only a st.number_input field without st.form, you cant reproduce this error. You need to test it inside st.form.
So I’ve tried changing my fields from st.number_input to st.text_input and just manually prompt the user if the value they added is not numeric, and if it’s numeric (float or int), i would convert it using the built-in python functions int() and/or float(). BUT the error still persists even after this, the values are still rounded off when passed by the form submit button.
Is there any way to stop st.form from rounding off values? for numeric values, the forms round off at the 7th number.
I do think it would be helpful to include a basic code example because even when I put the number input in a form I get the expected floating point value:
import streamlit as st
st.write("Inside the form")
var= st.number_input("Test Number", format="%.2f", value = 1.0, max_value=999999999999.00, min_value=0.00, key = 'number_slider')
# Every form must have a submit button.
submitted = st.form_submit_button("Submit",on_click = print_number)
EDIT: Deleted the link as there were some bugs.
The code above should work, however.
EDIT #2: Link may be working again. Please try here
Thank you very much for your replies @LukeF
After using your code in a separate instance of project, it does work!! So this is when I wondered which part in my code is different? - and that’s how I stored and checked the data. You stored and checked the data with session states and I checked and stored it with a database.
And so I realised the problem might’ve been how my database (phpMyAdmin) stored the data. I used “float” type instead of “double”. And I found this thread in stackoverflow. It shows my exact problem with the data being rounded off after the 6-7th unit.
“float” type has 6-7 significant digits
“double” type has 16 significant digits
TL; DR: My main issue lies with how my database stored the data. After changing it to “double” type, I’ve increased the precision and can now store larger values. I guess my problem for the future is when i need more than 16 significant digits, but this isnt a streamlit problem anymore. Thanks for helping me realise this @LukeF
Fantastic, glad it’s all sorted :–)