1

I am trying to print an integer in Vue2 with commas as thousands separators. For example, I want to show the number 34567 as "34 567". How would I go about doing this?

<v-carousel-item
        v-for="item in RANDOM_PRODUCTS"
        :key="item.id"
    >
 <span
    class="name_item_price"
 >
   {{ item.price[0].replace(/(\d)(?=(\d{3})+$)/g, '$1 ') }}
 </span>
</v-carousel-item>

This option used to work, but now it gives an error on some pages. Error:

Error in render: "TypeError: Cannot read property 'replace' of undefined"

1 Answer 1

1

Vue.js allows you to define filters that can be used to apply common text formatting.

<span
    class="name_item_price"
>
    {{ item.price[0] | capitalize }}
</span>

define a filter globally before creating the Vue instance: main.js

Vue.filter('capitalize', function (value) {
  if (!value) return ''
  value = value.toString()
  return value.replace(/(\d)(?=(\d{3})+$)/g, '$1 ')
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.