js memo

1、金额格式化 js

  1. function moneyFormat(money, digit) {
  2. digit = digit > 0 && digit <= 20 ? digit : 2;
  3. money = parseFloat(money).toFixed(digit);
  4. var integerArr = money.split(“.”)[0].split(“”).reverse();
  5. var decimals = money.split(“.”)[1];
  6. tempArr = “”;
  7. for (var i=0,k=integerArr.length;i<k;i++) {
  8. var cammaTag = (i+1)%3==0 && (i+1)!=k ? “,” : “”;
  9. tempArr += integerArr[i] + cammaTag;
  10. }
  11. money = tempArr.split(“”).reverse().join(“”) + “.” + decimals;
  12. return money;
  13. }