fix(slider): fix floating point decimal precision (#750)

* fix(slider): fix floating point decimal precision

* Update src/slider/src/Slider.tsx

Co-authored-by: 07akioni <07akioni2@gmail.com>

* Update src/slider/src/Slider.tsx

Co-authored-by: 07akioni <07akioni2@gmail.com>

* Update src/slider/src/Slider.tsx

Co-authored-by: 07akioni <07akioni2@gmail.com>

* fix(slider): fix floating point decimal precision

* fix(slider): update changelog

* Update CHANGELOG.en-US.md

Co-authored-by: Yugang Cao <34439652+Talljack@users.noreply.github.com>

* Update CHANGELOG.zh-CN.md

Co-authored-by: Yugang Cao <34439652+Talljack@users.noreply.github.com>

Co-authored-by: 07akioni <07akioni2@gmail.com>
Co-authored-by: Yugang Cao <34439652+Talljack@users.noreply.github.com>
This commit is contained in:
feng 2021-08-03 00:24:40 +08:00 committed by GitHub
parent b3e17cb160
commit 85de0ec37b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -114,6 +114,13 @@ export default defineComponent({
const railRef = ref<HTMLElement | null>(null)
const followerRef1 = ref<FollowerInst | null>(null)
const followerRef2 = ref<FollowerInst | null>(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 (