fix(slider): fix floating point precision issues in SingleSlider and DualRangeSlider

JavaScript floating point arithmetic causes values like 1.1 to become
1.1000000000000014. Add precision limiting to 2 decimal places in
snapToStep function for both sliders.
This commit is contained in:
Will Miao
2026-02-01 21:03:04 +08:00
parent 04ba966a6e
commit 337f73e711
4 changed files with 42 additions and 36 deletions

View File

@@ -206,7 +206,9 @@ const stepToDecimals = (step: number): number => {
const snapToStep = (value: number, segmentMultiplier?: number): number => {
const effectiveStep = segmentMultiplier ? props.step * segmentMultiplier : props.step
const steps = Math.round((value - props.min) / effectiveStep)
return Math.max(props.min, Math.min(props.max, props.min + steps * effectiveStep))
const rawValue = Math.max(props.min, Math.min(props.max, props.min + steps * effectiveStep))
// Fix floating point precision issues, limit to 2 decimal places
return Math.round(rawValue * 100) / 100
}
const startDrag = (handle: 'min' | 'max', event: PointerEvent) => {

View File

@@ -82,7 +82,9 @@ const stepToDecimals = (step: number): number => {
const snapToStep = (value: number): number => {
const steps = Math.round((value - props.min) / props.step)
return Math.max(props.min, Math.min(props.max, props.min + steps * props.step))
const rawValue = Math.max(props.min, Math.min(props.max, props.min + steps * props.step))
// Fix floating point precision issues, limit to 2 decimal places
return Math.round(rawValue * 100) / 100
}
const startDrag = (event: PointerEvent) => {