比如请求数据返回金额为10000,然后我们给做个金额格式分段;
1、我们首先模拟个数据~vue:data( ){ reutrn:{ num:10000 } } 原生js:var num=10000;
2、在项目中创建一个.js文件写入js方法,如下;
Number.prototype.formatMoney = function (places, thousand, decimal) {
places = !isNaN(places = Math.abs(places)) ? places : 2;
thousand = thousand || ",";
decimal = decimal || ".";
var number = this,
negative = number < 0 ? "-" : "",
i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "",
j = (j = i.length) > 3 ? j % 3 : 0;
return negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand) + (places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : "");
};
3、js使用可以直接写在方法外
console.log( num.formatMoney() )
4、vue可以将.js文件引入main文件,或者在本页面引入
import '../../assets/js/currency.js'
5、写入要转换的变量值
console.log( this.num.formatMoney() )