前端常用js方法集

const roundMoney = (money) =>{     //数字四舍五入,保留两位小数                                            
  return (Math.round(money * 100) / 100)
}
 roundMoney (17.855)       //17.86


const formatMoney = (value, digit) => {    // 金额格式化(数值=>金额)
  if (!value && value !== 0) return '-'
  if (digit === 4) {
    const formatNum = value.toFixed(digit)
    return formatNum.slice(0, formatNum.length - 2).replace(/B(?=(?:d{3})+(?!d))/g, ',') + formatNum.slice(formatNum.length - 2)
  }
  if (digit === 6) {
    const formatNum = value.toFixed(digit)
    return formatNum.slice(0, formatNum.length - 4).replace(/B(?=(?:d{3})+(?!d))/g, ',') + formatNum.slice(formatNum.length - 4)
  }
  return value.toFixed(2).replace(/B(?=(?:d{3})+(?!d))/g, ',')
}
 formatMoney (1,4)                  //1.0000       1元
 formatMoney (1,6)                 //1.000000      1元
 formatMoney(1000000)         //1,000,000.00   一百万


const formatNumber = (value) => {   // 金额格式化(金额=>数值)
  debugger;
  let count = Number(value.replace(/\,/g, ''))
  console.log(count,123)
  return Number(value.replace(/\,/g, ''))
}

 formatNumber (1.0)                             //1       1元
 formatMoney (1.00)                             //1       1元
 formatMoney (1,000,000.00)               //1000000    一百万



Date.prototype.format = function (format) {    // 日期格式化
  const o = {
    'M+': this.getMonth() + 1,
    'd+': this.getDate(),
    'h+': this.getHours(),
    'H+': this.getHours(),
    'm+': this.getMinutes(),
    's+': this.getSeconds(),
    'q+': Math.floor((this.getMonth() + 3) / 3),
    S: this.getMilliseconds(),
  }


  if (/(y+)/.test(format)) {    //  y+ :匹配任何包含至少一个y的字符串
    format = format.replace(RegExp.$1, `${this.getFullYear()}`.substr(4 - RegExp.$1.length))
  }
  for (let k in o) {
    if (new RegExp(`(${k})`).test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : (`00${o[k]}`).substr(`${o[k]}`.length))
    }
  }
  return format
}


 // 日期格式化使用方法
let now = new Date();     // Mon Sep 10 2018 14:11:51 GMT+0800 (中国标准时间1)
let nowStr = now.format("yyyy-MM-dd hh:mm:ss");
console.log('nowStr',nowStr,now)   // 2018-09-10 14:11:51


// 连字符转驼峰
String.prototype.hyphenToHump = function () {
  return this.replace(/-(w)/g, (...args) => {
    return args[1].toUpperCase()
  })
}


// 驼峰转连字符
String.prototype.humpToHyphen = function () {
  return this.replace(/([A-Z])/g, '-$1').toLowerCase()
}


// 向数组中添加不存在的元素
monitorWarehouses.forEach((item, i) => item.rowNo = i + 1)


// 千位符格式化
  方法一:
function toThousands(num) {
var result = '', counter = 0;
num = (num || 0).toString();
for (var i = num.length - 1; i >= 0; i--) {
counter++;
result = num.charAt(i) + result;
if (!(counter % 3) && i != 0) { result = ',' + result; }
}
return result;
}
toThousands(20000000000)


  方法二:
function toThousands(num) {
var result = [ ], counter = 0;
num = (num || 0).toString().split('');
for (var i = num.length - 1; i >= 0; i--) {
counter++;
result.unshift(num[i]);
if (!(counter % 3) && i != 0) { result.unshift(','); }
}
return result.join('');
}
toThousands(20000000000000000)


// 去掉字符串的一部分
var  fileBaseURL  = 'http://106.75.153.100:18080'
fileBaseURL = fileBaseURL.replace("http://", '')
console.log(fileBaseURL)        //   106.75.153.100:18080 


//  返回的值可以判断是否禁止
checkboxT (row) {
  if (row.status === 0 || row.status === 3 || row.status === 4) {
    return 1
  } else {
    return 0
  }
}
原文地址:https://www.cnblogs.com/IT123/p/10845651.html