diff --git a/CHANGELOG.en-US.md b/CHANGELOG.en-US.md index 98431af7bcb..cba336d952a 100644 --- a/CHANGELOG.en-US.md +++ b/CHANGELOG.en-US.md @@ -1,5 +1,13 @@ # CHANGELOG +## Pending + +### Feats + +### Fixes + +- Fix `n-slider` loss floating point decimal precision, closes [#751](https://github.com/TuSimple/naive-ui/issues/751). + ## 2.16.0 (2021-08-02) ### Breaking Changes diff --git a/CHANGELOG.zh-CN.md b/CHANGELOG.zh-CN.md index 8ed4c55680d..fbb19a3b2ae 100644 --- a/CHANGELOG.zh-CN.md +++ b/CHANGELOG.zh-CN.md @@ -1,5 +1,13 @@ # CHANGELOG +## Pending + +### Feats + +### Fixes + +- 修复 `n-slider` 丢失浮点数小数精度,关闭 [#751](https://github.com/TuSimple/naive-ui/issues/751) + ## 2.16.0 (2021-08-02) ### Breaking Changes diff --git a/src/slider/src/Slider.tsx b/src/slider/src/Slider.tsx index d8b8527fef8..d1768d7046b 100644 --- a/src/slider/src/Slider.tsx +++ b/src/slider/src/Slider.tsx @@ -114,6 +114,13 @@ export default defineComponent({ const railRef = ref(null) const followerRef1 = ref(null) const followerRef2 = ref(null) + const precisionRef = computed(() => { + const precisions = [props.min, props.max, props.step].map((item) => { + const fraction = String(item).split('.')[1] + return fraction ? fraction.length : 0 + }) + return Math.max(...precisions) + }) const uncontrolledValueRef = ref(props.defaultValue) const controlledValueRef = toRef(props, 'value') @@ -459,6 +466,7 @@ export default defineComponent({ justifiedValue = Math.max(min, justifiedValue) justifiedValue = Math.min(max, justifiedValue) justifiedValue = Math.round((justifiedValue - min) / step) * step + min + justifiedValue = parseFloat(justifiedValue.toFixed(precisionRef.value)) if (marks) { const closestMarkValue = getClosestMarkValue(value) if (