将金额数字每三位一逗号分隔

<div class="recharge-main-top">
  <p class="recharge-main-topp1">充值成功</p>
  <span class="recharge-main-topspan">+<span>1000.00</span>元</span>
  <p class="recharge-main-topp2">2017-12-25</p>
</div>
<div class="recharge-main-top">
  <p class="recharge-main-topp1">充值成功</p>
  <span class="recharge-main-topspan">+<span>500.00</span>元</span>
  <p class="recharge-main-topp2">2017-12-25</p>
</div>

 

function parseFormatNum(number,n){
  if(n != 0 ){
    n = (n > 0 && n <= 20) ? n : 2;
  }
  number = parseFloat((number + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
  var sub_val = number.split(".")[0].split("").reverse();
  var sub_xs = number.split(".")[1];

  var show_html = "";
  for (i = 0; i < sub_val.length; i++){
    show_html += sub_val[i] + ((i + 1) % 3 == 0 && (i + 1) != sub_val.length ? "," : "");
  }
  if(n == 0 ){
    return show_html.split("").reverse().join("");
  }else{
    return show_html.split("").reverse().join("") + "." + sub_xs;
  }
}

//测试
// console.log(parseFormatNum('15000.00',2));

 

//遍历所有金额同意改变形式
$(".recharge-main-topspan span").each(function(){
  // console.log($(this).text())
  var html= parseFormatNum($(this).text(),2);
  $(this).text(html);
});

posted @ 2017-12-23 13:49  xzzzys  Views(1284)  Comments(0Edit  收藏  举报