Skip to content

fix: round to correct precision when step uses exponential notation #8290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lucasweng
Copy link
Contributor

Closes #8274

✅ Pull Request Checklist:

  • Included link to corresponding React Spectrum GitHub Issue.
  • Added/updated unit tests and storybook for this change (for new code or code which already has tests).
  • Filled out test instructions.
  • Updated documentation (if it already exists for this component).
  • Looked at the Accessibility Practices for this feature - Aria Practices

📝 Test Instructions:

  • Added tests for roundToStepPrecision.
  • Existing tests should still pass.

🧢 Your Project:

expect(roundToStepPrecision(5, 10)).toBe(5);
});
it('should round to the correct decimal places for steps with decimals', () => {
expect(roundToStepPrecision(1.24, 0.1)).toBe(1.24);
Copy link
Member

@snowystinger snowystinger May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe I'm over thinking this, but shouldn't this be

Suggested change
expect(roundToStepPrecision(1.24, 0.1)).toBe(1.24);
expect(roundToStepPrecision(1.24, 0.1)).toBe(1.2);

I realize it's the existing behaviour, I'll have to look back to remember why

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into this! I was wondering the same thing while looking at the original implementation 🤔

let precision = pointIndex >= 0 ? stepString.length - pointIndex : 0;

I also thought precision should maybe be stepString.length - pointIndex - 1, but the original behavior seemed correct, since all the existing test cases in NumberField.test and number.test passed. Also, this #8274 (comment) suggests that for an exponential notation like 1e-7, the precision should be 8. So, I implemented the existing behavior for exponential notation as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NumberField, useNumberField step precision bug
2 participants