🐛(component): 修复 input-number 小数精度问题

This commit is contained in:
就眠儀式 2022-11-10 09:10:18 +08:00
parent 01400f38e5
commit cc1f6de1b5
6 changed files with 46 additions and 15 deletions

View File

@ -89,7 +89,7 @@ const addition = function () {
}; };
const subtraction = function () { const subtraction = function () {
num.value = Number(sub(num.value, props.step)); num.value = sub(num.value, props.step);
}; };
const longDown = function (fn: Function) { const longDown = function (fn: Function) {

View File

@ -1,6 +1,6 @@
//加法 //加法
function add(arg1: number, arg2: number) { function add(arg1: number, arg2: number) {
var r1, r2, m; var r1, r2, m, c;
try { try {
r1 = arg1.toString().split(".")[1].length; r1 = arg1.toString().split(".")[1].length;
} catch (e) { } catch (e) {
@ -11,12 +11,26 @@ function add(arg1: number, arg2: number) {
} catch (e) { } catch (e) {
r2 = 0; r2 = 0;
} }
c = Math.abs(r1 - r2);
m = Math.pow(10, Math.max(r1, r2)); m = Math.pow(10, Math.max(r1, r2));
return (arg1 * m + arg2 * m) / m; if (c > 0) {
var cm = Math.pow(10, c);
if (r1 > r2) {
arg1 = Number(arg1.toString().replace(".", ""));
arg2 = Number(arg2.toString().replace(".", "")) * cm;
} else {
arg1 = Number(arg1.toString().replace(".", "")) * cm;
arg2 = Number(arg2.toString().replace(".", ""));
} }
} else {
arg1 = Number(arg1.toString().replace(".", ""));
arg2 = Number(arg2.toString().replace(".", ""));
}
return (arg1 + arg2) / m;
};
//减法 //减法
function sub(arg1: number, arg2: number) { function sub(arg1: number, arg2: number) {
var r1, r2, m, n; var r1, r2, m, c;
try { try {
r1 = arg1.toString().split(".")[1].length; r1 = arg1.toString().split(".")[1].length;
} catch (e) { } catch (e) {
@ -27,8 +41,21 @@ function sub(arg1: number, arg2: number) {
} catch (e) { } catch (e) {
r2 = 0; r2 = 0;
} }
c = Math.abs(r1 - r2);
m = Math.pow(10, Math.max(r1, r2)); m = Math.pow(10, Math.max(r1, r2));
n = r1 >= r2 ? r1 : r2; if (c > 0) {
return ((arg1 * m - arg2 * m) / m).toFixed(n); var cm = Math.pow(10, c);
if (r1 > r2) {
arg1 = Number(arg1.toString().replace(".", ""));
arg2 = Number(arg2.toString().replace(".", "")) * cm;
} else {
arg1 = Number(arg1.toString().replace(".", "")) * cm;
arg2 = Number(arg2.toString().replace(".", ""));
} }
} else {
arg1 = Number(arg1.toString().replace(".", ""));
arg2 = Number(arg2.toString().replace(".", ""));
}
return (arg1 - arg2) / m;
};
export { add, sub }; export { add, sub };

View File

@ -110,7 +110,6 @@ const doHidden = function (e?: MouseEvent) {
}; };
const calcPosistion = function () { const calcPosistion = function () {
postionFns[props.position] && postionFns[props.position] &&
(style.value = postionFns[props.position]( (style.value = postionFns[props.position](
triggerRefEl.value, triggerRefEl.value,
@ -121,7 +120,9 @@ const calcPosistion = function () {
const updatePosistion = function () { const updatePosistion = function () {
if (innerVisible.value) { if (innerVisible.value) {
popperRefEl.value.offsetWidth === 0 ? nextTick(() => calcPosistion()) : calcPosistion(); popperRefEl.value.offsetWidth === 0
? nextTick(() => calcPosistion())
: calcPosistion();
nextTick(() => { nextTick(() => {
calcPosistion(); calcPosistion();
}); });

View File

@ -105,8 +105,12 @@ const clearAllCutEffect = () => {
}; };
const { t } = useI18n(); const { t } = useI18n();
const text = computed(() =>{ return props.text ? props.text : t("upload.text")}); const text = computed(() => {
const dragText = computed(() => { return props.dragText ? props.dragText : t("upload.dragText")}); return props.text ? props.text : t("upload.text");
});
const dragText = computed(() => {
return props.dragText ? props.dragText : t("upload.dragText");
});
const defaultErrorMsg = computed(() => t("upload.defaultErrorMsg")); const defaultErrorMsg = computed(() => t("upload.defaultErrorMsg"));
const urlErrorMsg = computed(() => t("upload.urlErrorMsg")); const urlErrorMsg = computed(() => t("upload.urlErrorMsg"));
const numberErrorMsg = computed(() => t("upload.numberErrorMsg")); const numberErrorMsg = computed(() => t("upload.numberErrorMsg"));

View File

@ -13,8 +13,7 @@
::: demo 使用 `lay-input-number` 标签, 创建数字输入框 ::: demo 使用 `lay-input-number` 标签, 创建数字输入框
<template> <template>
{{data1}} <lay-input-number v-model="data1" :step="0.11"></lay-input-number>
<lay-input-number v-model="data1" :step="0.1"></lay-input-number>
<lay-input-number v-model="data2" position="right"></lay-input-number> <lay-input-number v-model="data2" position="right"></lay-input-number>
</template> </template>