Skip to content

Commit aff3025

Browse files
author
Dipak Sarkar
committed
updated number formatter
1 parent 7842670 commit aff3025

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

src/component.vue

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ export default {
3535
type: Number,
3636
default: () => options.precision
3737
},
38+
minimumFractionDigits: {
39+
type: [Number, Boolean],
40+
default: () => options.minimumFractionDigits
41+
},
3842
decimal: {
3943
type: String,
4044
default: () => options.decimal
@@ -66,7 +70,6 @@ export default {
6670
input({ target }) {
6771
this.maskedValue = target.value
6872
this.unmaskedValue = target.unmaskedValue
69-
this.$emit('update:modelValue', this.emittedValue)
7073
},
7174
change() {
7275
this.$emit('update:modelValue', this.emittedValue)
@@ -79,6 +82,13 @@ export default {
7982
config() {
8083
return this.$props
8184
}
82-
}
85+
},
86+
watch: {
87+
modelValue (val) {
88+
if (this.unmaskedValue !== val) {
89+
this.maskedValue = val
90+
}
91+
}
92+
},
8393
}
8494
</script>

src/number-format.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,7 @@ export default function NumberFormat(config = options) {
4848
if (this.options.reverseFill) {
4949
this.number = toFixed(this.numberOnly(this.input, /\D+/g), this.options.precision).replace('.', this.options.decimal)
5050
} else if (typeof this.input === 'number') {
51-
if (this.isClean) {
52-
this.number = this.toNumber(this.input.toFixed(this.options.precision)).toString().replace('-', '').replace('.', this.options.decimal)
53-
} else {
54-
this.number = this.toNumber(this.input).toString().replace('-', '').replace('.', this.options.decimal)
55-
}
51+
this.number = this.parts(this.input.toString().replace('-', ''), '.').join(this.options.decimal)
5652
} else {
5753
this.number = this.numberOnly(this.input, new RegExp(`[^0-9\\${this.options.decimal}]+`, 'gi'))
5854
this.number = this.parts(this.number).join(this.options.decimal)
@@ -71,23 +67,30 @@ export default function NumberFormat(config = options) {
7167
this.parts = (number = '', decimal = this.options.decimal) => {
7268
var parts = number.toString().split(decimal)
7369
parts[0] = this.toNumber(parts[0]) || 0
70+
7471
if (parts.length > 1) {
7572
parts[1] = parts.slice(1, parts.length).join('')
7673
parts = parts.slice(0, 2)
77-
if (this.isClean && parts[1].length) {
78-
parts[1] = this.toNumber(`.${parts[1]}`).toFixed(this.options.precision).toString().replace('0.', '')
74+
}
75+
76+
if (this.isClean) {
77+
const newNumber = this.toNumber(parts.join('.')).toFixed(this.options.precision)
78+
const cleanNumber = this.toNumber(newNumber)
79+
const minimumDigits = cleanNumber.toFixed(this.options.minimumFractionDigits)
80+
81+
if (this.options.minimumFractionDigits && this.options.minimumFractionDigits >= 0 && cleanNumber.toString().length < minimumDigits.length) {
82+
parts = minimumDigits.toString().split('.')
83+
} else {
84+
parts = cleanNumber.toString().split('.')
7985
}
8086
}
87+
8188
return parts.slice(0, 2)
8289
}
8390

8491
this.addSeparator = () => {
8592
var parts = this.numbers().split(this.options.decimal)
8693
parts[0] = parts[0].toString().replace(/(\d)(?=(?:\d{3})+\b)/gm, `$1${this.options.separator}`)
87-
if (this.isClean) {
88-
parts[1] = this.toNumber(`.${parts[1]}`).toString().replace('0.', '')
89-
return parts[1] && parts[1] > 0 ? parts.join(this.options.decimal) : parts[0]
90-
}
9194
return parts.join(this.options.decimal)
9295
}
9396

src/options.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export default {
44
separator: ',',
55
decimal: '.',
66
precision: 2,
7+
minimumFractionDigits: false,
78
prefill: true,
89
reverseFill: false,
910
min: false,

0 commit comments

Comments
 (0)