Dmitry Bubnenkov
Dmitry Bubnenkov

Reputation: 9859

How to add space between digits of number?

I have got input field where user can type: 10000, but I want to make it's displayed as 10 000. How it's possible to do it with vue?

Upvotes: 0

Views: 2240

Answers (1)

Nikola Kirincic
Nikola Kirincic

Reputation: 3757

Create custom filter that splits your number by 3 digits from end ( if there are more than 3 digits ) using regular expression, and add it to your app:

Vue.filter('splitNumber', function (value) {
   return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ");
})

Then just use it as filter:

<span v-text="yourNumber | splitNumber"></span>

Upvotes: 1

Related Questions