Improve st.number_input precision#13484
Conversation
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
✅ PR preview is ready!
|
📈 Frontend coverage change detectedThe frontend unit test (vitest) coverage has increased by 0.0200%
✅ Coverage change is within normal range. |
st.number_input precisionst.number_input precision
3a2ad8a to
d547037
Compare
There was a problem hiding this comment.
Pull request overview
This PR fixes floating point arithmetic errors in st.number_input that occurred during increment/decrement operations. The key improvement is the introduction of precise step arithmetic using scale-based integer math to avoid issues like 0.1 + 0.01 = 0.11000000000000001. Additionally, the PR adds support for very small step values that JavaScript represents in scientific notation (e.g., 1e-7), which previously failed completely.
Key changes:
- New
getDecimalPlacesfunction to extract decimal precision from both standard and scientific notation - New
preciseStepArithmeticfunction to perform accurate increment/decrement operations - Updated
formatValueto usegetDecimalPlacesfor consistent precision handling - Comprehensive test coverage across unit, integration, and E2E tests
Reviewed changes
Copilot reviewed 6 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
frontend/lib/src/components/widgets/NumberInput/utils.ts |
Added getDecimalPlaces and preciseStepArithmetic functions; updated formatValue to use new precision logic |
frontend/lib/src/components/widgets/NumberInput/utils.test.ts |
Added comprehensive unit tests for new functions covering standard decimals, scientific notation, and edge cases |
frontend/lib/src/components/widgets/NumberInput/NumberInput.tsx |
Integrated preciseStepArithmetic into increment/decrement callbacks |
frontend/lib/src/components/widgets/NumberInput/NumberInput.test.tsx |
Added integration tests verifying precise arithmetic through button clicks and keyboard interactions |
e2e_playwright/st_number_input.py |
Added two new number inputs (18 and 19) for testing scientific notation steps |
e2e_playwright/st_number_input_test.py |
Added four E2E tests for floating point precision and scientific notation handling |
e2e_playwright/__snapshots__/linux/st_number_input_test/st_number_input-value_none_min_1[dark_theme-chromium].png |
Updated snapshot reflecting the increased number input count |
sfc-gh-nbellante
left a comment
There was a problem hiding this comment.
Thanks for doing this!! 🙌 It's been bothering me forever
Describe your changes
When using the increment/decrement buttons or arrow keys on

st.number_input, floating point arithmetic errors could cause values to display incorrectly:Before:
This PR improves increment/decrement with precise calculation, handling both standard notation (
0.01) and scientific notation (1e-7).Note: scientific notation (very small step values) failed completely in number input before because JS automatically converts very small #s to scientific notation - this PR solves that bug as well.
Testing Plan