Skip to content
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

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

Merged
merged 8 commits into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.en-US.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/slider/src/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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 (
Expand Down