JavaScript使用技巧汇总

1.计算百分比

function toPercent() {
   Number.prototype.toPercent = function(){
      return (Math.round(this * 10000)/100).toFixed(2) + '%';
   }
}

2.将数字转换为金额格式

function toMoney(num){
    num = num.toFixed(2);  //将数字转成带有2位小数的字符串
    num = parseFloat(num)  // 将带有2位小数的字符串转成带有小数的数字
    num = num.toLocaleString(); // 将带有2位小数的数字转成金额格式
    return num;
}

猜你喜欢

转载自blog.csdn.net/weijizhu1000/article/details/80676482