[number field] Preserve full numeric precision by default#5040
Draft
atomiks wants to merge 2 commits into
Draft
Conversation
The stored value was rounded to 3 fraction digits when no Intl rounding options were provided, an accidental default inherited from Intl.NumberFormat's resolved maximumFractionDigits. This truncated committed values (a regression from v1.5.0, which committed full precision on blur) and made step/smallStep values finer than 0.001 silent no-ops. Clean only binary floating-point noise in the default branch instead, preserving legitimate precision. The Intl round-trip branch for explicit rounding options is unchanged.
commit: |
Bundle size
PerformanceTotal duration: 847.39 ms ▼-606.85 ms(-41.7%) | Renders: 50 (+0) | Paint: 1,297.20 ms ▼-887.19 ms(-40.6%)
…and 7 more — details Check out the code infra dashboard for more information about this PR. |
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Values typed with more than 15 significant digits were rounded by the toPrecision(15) noise cleanup, so a no-format field could report and commit a different value than the user entered. Parsed input (typing, paste, blur) involves no arithmetic — percent and permille scaling shift the decimal exponent as a string — so it carries no binary noise and is now returned verbatim. Cleanup still applies to stepping and snapping, whose arithmetic can introduce noise; a flat cap preserving 16 digits could not clean 16-digit noise patterns such as 0.1 + 0.7 === 0.7999999999999999.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Restores v1.5.0's committed-value precision and fixes
step/smallStepvalues finer than0.001being silent no-ops.Root cause
removeFloatingPointErrorsrounded the stored numeric value to 3 fraction digits wheneverformathad no Intl rounding options. The 3-digit default was accidental: the original implementation intended a full-precision fallback (format?.maximumFractionDigits ?? defaultOptions.maximumFractionDigits ?? 20), butnew Intl.NumberFormat().resolvedOptions().maximumFractionDigitsis3, so the?? 20never applied.Consequences:
step={0.0001}(orsmallStepbelow0.001) was a silent no-op: incrementing from0rounded0.0001back to0, soonValueChangenever fired and the value never advanced. Native<input type="number" step="0.0001">works fine.onValueCommittedreported the rounded value (1.235when typing1.23456) instead of full precision as in v1.5.0.Changes
The default branch now cleans only binary floating-point noise (
0.1 + 0.2→0.3) viatoPrecision(15)instead of truncating to 3 digits. ANumber.isIntegerguard returns safe-range integers verbatim, sincetoPrecision(15)would corrupt 16-digit integers likeNumber.MAX_SAFE_INTEGER.The Intl round-trip branch for explicit rounding options (#4804) is unchanged:
roundingMode, significant digits,roundingIncrement, and percent display-scale rounding on blur are still respected (covered by the existing tests, all passing).Behavior comparison (no
format, typing1.23456)onValueCommittedon blur1.234561.2351.23456onValueChangewhile typing1.2351.2351.234561.2351.2351.23456+1from1.234562.2352.2352.23456step={0.0001}from0v1.5.0 was internally split: it committed full precision on blur while storing and reporting 3-digit rounded values. Since the step no-op and the stored-value rounding share the same root cause, the stored value now uniformly keeps full precision — matching what v1.5.0 already committed and how the native number input steps. Rounding the displayed value remains the job of
format.Tests
0.001stepping advances and firesonValueChange; typed high-precision values are reported in full byonValueChange/onValueCommitted; unit tests for noise cleanup, precision preservation, and safe-integer exactness invalidate.test.ts.