工具函数 长期更新

四舍五入: 


//四舍五入方法,num为处理的数值,point为保留位数  
export let toFixed = function (num, point) {
  //取要保留位数后的一位
  console.log(parseFloat((num * Math.pow(10, point)).toFixed(point)));
  var endNum = parseInt(num * Math.pow(10, (point + 1))) % 10;
  if (endNum <= 4) {
    return parseInt(parseFloat((num * Math.pow(10, point)).toFixed(point))) / Math.pow(10, point);
  } else {
    return (parseInt(parseFloat((num * Math.pow(10, point)).toFixed(point))) + 1) / Math.pow(10, point);
  }
}

//四舍五入方法2
export let round = function (num, decimal) {
  if (isNaN(num)) {
    return 0;
  }
  const p1 = Math.pow(10, decimal + 1);
  const p2 = Math.pow(10, decimal);
  return Math.round(num * p1 / 10) / p2;
}
/**
 *  电话号码脱敏
 * 
 */
export let toHide = function(array) {
	var mphone = array.substring(0, 3) + '****' + array.substring(7);
	return mphone;
}
/* 日期格式化 */
export let dateFormat = function(date, fmt = 'yyyy-MM-dd hh:mm:ss') {
	date = new Date(date)
	var o = {
		"M+": date.getMonth() + 1, //月份
		"d+": date.getDate(), //日
		"h+": date.getHours(), //小时
		"m+": date.getMinutes(), //分
		"s+": date.getSeconds(), //秒
		"q+": Math.floor((date.getMonth() + 3) / 3), //季度
		"S": date.getMilliseconds() //毫秒
	};
	if (/(y+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
	}
	for (var k in o) {
		if (new RegExp("(" + k + ")").test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
		}
	}
	return fmt;
}

拆分省市区

var str = "湖北省武汉市江夏区文化大道110号";
console.log(that.getArea(str));

//省市区截取
getArea: function(str) {
  let area = {}
  let index11 = 0
  let index1 = str.indexOf("省")
  if (index1 == -1) {
    index11 = str.indexOf("自治区")
    if (index11 != -1) {
      area.Province = str.substring(0, index11 + 3)
    } else {
      area.Province = str.substring(0, 0)
    }
  } else {
    area.Province = str.substring(0, index1 + 1)
  }

  let index2 = str.indexOf("市")
  if (index11 == -1) {
    area.City = str.substring(index11 + 1, index2 + 1)
  } else {
    if (index11 == 0) {
      area.City = str.substring(index1 + 1, index2 + 1)
    } else {
      area.City = str.substring(index11 + 3, index2 + 1)
    }
  }

  let index3 = str.lastIndexOf("区")
  if (index3 == -1) {
    index3 = str.indexOf("县")
    area.Country = str.substring(index2 + 1, index3 + 1)
  } else {
    area.Country = str.substring(index2 + 1, index3 + 1)
  }
  return area;
}

方法二:

var str = "湖北省武汉市江夏区文化大道110号";
var reg = /.+?(省|市|自治区|自治州|自治县|自治旗|县|旗|区|群岛|海域|新界|九龙|岛)/g; // 省市区的正则
console.log(str.match(reg));

猜你喜欢

转载自blog.csdn.net/m0_57033755/article/details/130373313