mirror of
https://gitee.com/element-plus/element-plus.git
synced 2024-12-02 03:08:21 +08:00
fix: input-number precision accuracy (#7398)
Enhanced precision processing.
This commit is contained in:
parent
d04c466e36
commit
654428d144
@ -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" />',
|
||||
|
@ -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) => {
|
||||
|
Loading…
Reference in New Issue
Block a user