Skip to content

Commit 93d179c

Browse files
fix(): Parse number to string correctly
1 parent 2b957e2 commit 93d179c

File tree

2 files changed

+96
-3
lines changed

2 files changed

+96
-3
lines changed

spec/vue_numeric.spec.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,87 @@ describe("vue-numeric", function() {
4949

5050
});
5151

52+
it('updates values decimals', done => {
53+
const vm = new Vue({
54+
el,
55+
data: function() {
56+
return {
57+
total: 0
58+
}
59+
},
60+
template: `<div>
61+
<vue-numeric v-model="total" separator="." precision="2"></vue-numeric>
62+
<span>{{total}}</span>
63+
</div>`,
64+
components: { VueNumeric }
65+
}).$mount()
66+
67+
vm.total = 3000;
68+
69+
Vue.nextTick(() => {
70+
const span = vm.$el.getElementsByTagName('span')[0];
71+
expect(span.textContent.trim()).toEqual('3000');
72+
expect(vm.$el.firstChild.value.trim()).toEqual('3.000,00')
73+
done()
74+
});
75+
76+
});
77+
78+
79+
it('accepts decimal values', done => {
80+
const vm = new Vue({
81+
el,
82+
data: function() {
83+
return {
84+
total: 200.22,
85+
subtotal: "110.98",
86+
large: "10.000,1"
87+
};
88+
},
89+
template: `<div>
90+
<vue-numeric v-model="large" separator="." precision="2"></vue-numeric>
91+
<vue-numeric v-model="total" separator="." precision="2"></vue-numeric>
92+
<vue-numeric v-model="subtotal" precision="2"></vue-numeric>
93+
</div>`,
94+
components: { VueNumeric }
95+
}).$mount()
96+
97+
Vue.nextTick(() => {
98+
expect(vm.$el.children[0].value.trim()).toEqual('10.000,10')
99+
expect(vm.$el.children[1].value.trim()).toEqual('200,22')
100+
expect(vm.$el.children[2].value.trim()).toEqual('110.98')
101+
done()
102+
});
103+
104+
});
105+
106+
107+
it('updates values with correct separator', done => {
108+
const vm = new Vue({
109+
el,
110+
data: function() {
111+
return {
112+
total: 0
113+
}
114+
},
115+
template: `<div>
116+
<vue-numeric v-model="total" separator="."></vue-numeric>
117+
<span>{{total}}</span>
118+
</div>`,
119+
components: { VueNumeric }
120+
}).$mount()
121+
122+
vm.total = 3000;
123+
124+
Vue.nextTick(() => {
125+
const span = vm.$el.getElementsByTagName('span')[0];
126+
expect(span.textContent.trim()).toEqual('3000');
127+
expect(vm.$el.firstChild.value.trim()).toEqual('3.000')
128+
done()
129+
});
130+
131+
});
132+
52133

53134
it('updates value without format', done => {
54135
const vm = new Vue({

src/vue-numeric.vue

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,11 @@ export default {
183183
let number = 0
184184
185185
if (this.separator === '.') {
186-
number = Number(String(value).replace(/[^0-9-,]+/g, '').replace(',', '.'))
186+
let cleanValue = value;
187+
if (typeof value != 'string') {
188+
cleanValue = this.numberToString(value);
189+
}
190+
number = Number(String(cleanValue).replace(/[^0-9-,]+/g, '').replace(',', '.'))
187191
} else {
188192
number = Number(String(value).replace(/[^0-9-.]+/g, ''))
189193
}
@@ -232,13 +236,21 @@ export default {
232236
* Remove symbol and separator.
233237
* @param {Number} value
234238
*/
235-
convertToNumber (value) {
236-
this.amount = accounting.formatMoney(value, {
239+
numberToString (value) {
240+
return accounting.formatMoney(value, {
237241
symbol: '',
238242
precision: Number(this.precision),
239243
decimal: this.decimalSeparator,
240244
thousand: ''
241245
})
246+
},
247+
248+
/**
249+
* Remove symbol and separator.
250+
* @param {Number} value
251+
*/
252+
convertToNumber (value) {
253+
this.amount = this.numberToString(value);
242254
}
243255
},
244256

0 commit comments

Comments
 (0)