1
0
mirror of https://gitee.com/vant-contrib/vant-weapp.git synced 2025-04-06 03:58:05 +08:00

fix(Slider): fix decimal value

This commit is contained in:
johnsonwong666 2023-06-19 20:26:53 +08:00
parent 7decaed8e4
commit e8ed007d89
2 changed files with 17 additions and 3 deletions
packages
common
slider

@ -96,6 +96,15 @@ export function toPromise(promiseLike: Promise<unknown> | unknown) {
return Promise.resolve(promiseLike);
}
// 浮点数精度处理
export function addNumber(num1, num2) {
const cardinal = 10 ** 10;
return Math.round((num1 + num2) * cardinal) / cardinal;
}
// 限制value在[min, max]之间
export const clamp = (num, min, max) => Math.min(Math.max(num, min), max);
export function getCurrentPage<T>() {
const pages = getCurrentPages();
return pages[pages.length - 1] as T & WechatMiniprogram.Page.TrivialInstance;

@ -1,7 +1,7 @@
import { VantComponent } from '../common/component';
import { touch } from '../mixins/touch';
import { canIUseModel } from '../common/version';
import { getRect, addUnit, nextTick } from '../common/utils';
import { getRect, addUnit, nextTick, addNumber, clamp } from '../common/utils';
type SliderValue = number | [number, number];
@ -238,8 +238,13 @@ VantComponent({
},
format(value: number) {
const { max, min, step } = this.data;
return Math.round(Math.max(min, Math.min(value, max)) / step) * step;
const min = +this.data.min;
const max = +this.data.max;
const step = +this.data.step;
value = clamp(value, min, max);
const diff = Math.round((value - min) / step) * step;
return addNumber(min, diff);
},
},
});