Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format numbers in VueJS

I couldn't find a way of formatting numbers in VueJS. All I found was the builtin currency filter and vue-numeric for formatting currencies, which needs some modification to look like a label. And then you can't use it for displaying iterated array members.

like image 834
Buffalo Avatar asked Jun 14 '17 07:06

Buffalo


2 Answers

Install numeral.js:

npm install numeral --save   

Define the custom filter:

<script>   var numeral = require("numeral");    Vue.filter("formatNumber", function (value) {     return numeral(value).format("0,0"); // displaying other groupings/separators is possible, look at the docs   });    export default   {     ...   }  </script> 

Use it:

<tr v-for="(item, key, index) in tableRows">   <td>{{item.integerValue | formatNumber}}</td> </tr> 
like image 196
Buffalo Avatar answered Sep 28 '22 09:09

Buffalo


Intl.NumberFormat(), default usage:

... created: function() {     let number = 1234567;     console.log(new Intl.NumberFormat().format(number)) }, ...  //console -> 1 234 567 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat

like image 27
Naskalin Avatar answered Sep 28 '22 09:09

Naskalin