【VUE】js将number数字转换为金额格式,类似于10000=10,000.00

比如请求数据返回金额为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() )

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容