My Vue component is like this :
<template>
<div>
<div class="panel-group"v-for="item in list">
<div class="col-md-8">
<small>
Total: <b>{{ item.total }}</b>
</small>
</div>
</div>
</div>
</template>
<script>
export default {
...
computed: {
list: function() {
return this.$store.state.transaction.list
},
...
}
}
</script>
The result of {{ item.total }}
is
26000000
But I want format it to be like this :
26.000.000,00
In jquery or javascript, I can do it
But, How to do it in vue component?
You can format currency writing your own code but it is just solution for the moment - when your app will grow you can need other currencies.
There is another issue with this:
I think the best option is use complex solution for internationalization e.g. library vue-i18n( http://kazupon.github.io/vue-i18n/).
I use this plugin and I don't have to worry about such a things. Please look at documentation - it is really simple:
http://kazupon.github.io/vue-i18n/guide/number.html
so you just use:
and set EN-us to get $100.00:
or set PL to get 100,00 zł:
This plugin also provide different features like translations and date formatting.
The comment by @RoyJ has a great suggestion. In the template you can just use built-in localized strings:
It's not supported in some of the older browsers, but if you're targeting IE 11 and later, you should be fine.
There is issues with the precision of the accepted answer.
the round(value, decimals) function in this test works. unlike the simple toFixed example.
this is a test of the toFixed vs round method.
http://www.jacklmoore.com/notes/rounding-in-javascript/
mixin example
I would write method for that, and then where you need to format price you can just put method in template and pass value down
And then in template:
BTW - I didn't put too much care on replacing and regular expression.It could be improved.
With vuejs 2, you could use vue2-filters which does have other goodies as well.
Then use it like so:
Ref: https://www.npmjs.com/package/vue2-filters
I have created a filter. The filter can be used in any page.
Then I can use this filter like this:
I used these related answers to help with the implementation of the filter: