MCPcopy Index your code
hub / github.com/adobe/react-spectrum / roundToStepPrecision

Function roundToStepPrecision

packages/react-stately/src/utils/number.ts:22–41  ·  view source on GitHub ↗
(value: number, step: number)

Source from the content-addressed store, hash-verified

20}
21
22export function roundToStepPrecision(value: number, step: number): number {
23 let roundedValue = value;
24 let precision = 0;
25 let stepString = step.toString();
26 // Handle negative exponents in exponential notation (e.g., "1e-7" → precision 8)
27 let eIndex = stepString.toLowerCase().indexOf('e-');
28 if (eIndex > 0) {
29 precision = Math.abs(Math.floor(Math.log10(Math.abs(step)))) + eIndex;
30 } else {
31 let pointIndex = stepString.indexOf('.');
32 if (pointIndex >= 0) {
33 precision = stepString.length - pointIndex;
34 }
35 }
36 if (precision > 0) {
37 let pow = Math.pow(10, precision);
38 roundedValue = Math.round(roundedValue * pow) / pow;
39 }
40 return roundedValue;
41}
42
43export function snapValueToStep(
44 value: number,

Callers 2

number.test.tsFile · 0.90
snapValueToStepFunction · 0.85

Calls 1

toStringMethod · 0.65

Tested by

no test coverage detected