diff --git a/packages/components/input-number/__tests__/input-number.test.ts b/packages/components/input-number/__tests__/input-number.test.ts index 5cc93f7cce..f0b03f9b86 100644 --- a/packages/components/input-number/__tests__/input-number.test.ts +++ b/packages/components/input-number/__tests__/input-number.test.ts @@ -158,6 +158,19 @@ describe('InputNumber.vue', () => { await wrapper.find('input').setValue(1.1111111111) expect(wrapper.find('input').element.value).toEqual('1.11') }) + test('precision accuracy', async () => { + const wrapper = _mount({ + template: '', + setup() { + const num = ref(0) + return { + num, + } + }, + }) + await wrapper.find('input').setValue(17.275) + expect(wrapper.find('input').element.value).toEqual('17.28') + }) test('disabled', async () => { const wrapper = _mount({ template: '', diff --git a/packages/components/input-number/src/input-number.vue b/packages/components/input-number/src/input-number.vue index 40dce765d3..6eaa83afaa 100644 --- a/packages/components/input-number/src/input-number.vue +++ b/packages/components/input-number/src/input-number.vue @@ -153,6 +153,12 @@ export default defineComponent({ }) const toPrecision = (num: number, pre?: number) => { if (isUndefined(pre)) pre = numPrecision.value + const digits = num.toString().split('.') + if (digits.length > 1) { + const integer = digits[0] + const decimal = Math.round(+digits[1] / 10 ** (digits[1].length - pre)) + return Number.parseFloat(`${integer}.${decimal}`) + } return Number.parseFloat(`${Math.round(num * 10 ** pre) / 10 ** pre}`) } const getPrecision = (value: number | undefined) => {