fix: input-number precision accuracy (#7398)

Enhanced precision processing.
This commit is contained in:
Xc 2022-05-02 16:38:08 +08:00 committed by GitHub
parent d04c466e36
commit 654428d144
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 0 deletions

View File

@ -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: '<el-input-number :precision="2" v-model="num" />',
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: '<el-input-number :disabled="true" v-model="num" />',

View File

@ -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) => {