/**
* 判断当前值是否为不为空的数字
* @param {数字} num
*/
static isNumber(num){
if(num == "") return false;
if(isNaN(num) || parseFloat(num).toString() == "NaN"){
return false
}else{
return true;
}
}
/**
* 为一个数字增加千分位
* @param {*} num
*/
static thousandsForamt(num){
if(!this.isNumber(num)) return num
var res = num.toString().replace(/\d+/, function (n) { // 先提取整数部分
return n.replace(/(\d)(?=(\d{3})+$)/g, function ($1) {
return $1 + ','
})
})
return res
}
/**
* 为所有传进来的值小数补零
* @param {需要补0的数字} num
* @param {补零的个数,如果不传则默认为两位} digitNum
*/
static fillNumber (num, digitNum) {
if (!this.isNumber(num)) {
return num
}
var digit = ''
var round = ''
if (num === null || num === undefined || num === '') {
num = 0.00
}
var numStr = num.toString()
if (numStr.indexOf('.') != -1) {
round = numStr.split('.')[0]
digit = numStr.split('.')[1]
} else {
round = numStr
}
for (var i = digitNum - digit.length; i > 0; i--) {
digit = digit + '0'
}
return round + '.' + digit
}
/**
* 将数字转换为人民币并返回单位
* @param {*} num
*/
static getFormatMoney(num) {
var ret = {}
if (num >= 100000000) {
ret.num = Math.round((num / 100000000) * 100) / 100
ret.unit = '亿元'
} else if (num >= 10000) {
ret.num = Math.round((num / 10000) * 100) / 100
ret.unit = '万元'
} else if (num > 0) {
ret.num = num
ret.unit = '元'
} else if (num < 0) {
// 负数
var num2 = 0 - num
if (num2 >= 100000000) {
ret.num = 0 - Math.round((num2 / 100000000) * 100) / 100
ret.unit = '亿元'
} else if (num2 >= 10000) {
ret.num = 0 - Math.round((num2 / 10000) * 100) / 100
ret.unit = '万元'
} else if (num2 >= 0) {
ret.num = 0 - num
ret.unit = '元'
}
} else {
ret.num = '0'
ret.unit = '元'
}
ret.num = Math.round(ret.num * 100) / 100
return ret
}
// 下面两个一起用 可以统一单位
function getMaxNum (arr) {
const arr2 = []
for (let i in arr) {
if (arr[i] == undefined) {
arr[i] = 0
}
arr2.push(Math.abs(arr[i]))
}
var index = arr2.indexOf(Math.max.apply(null, arr2))
// console.log('max=====' + arr[index])
return arr2[index]
}
function getFormatMoney (num, unit) {
var ret = {}
// console.log(unit);
if (num == '') {
ret.num = 0
ret.unit = 1
ret.unitName = '元'
return ret
}
var computenum = num
if (num <= 0) {
computenum = 0 - num
}
if (!unit || unit === 0) {
if (computenum >= 100000000) {
unit = 100000000
} else if (computenum >= 10000) {
unit = 10000
} else {
unit = 1
}
// unit = 10000; // 默认取万元
}
switch (unit) {
case 10000:
ret.num = Math.round((num / 10000) * 100) / 100
ret.unitName = '万元'
ret.unit = unit
break
case 100000000:
ret.num = Math.round((num / 100000000) * 100) / 100
ret.unitName = '亿元'
ret.unit = unit
break
default:
ret.num = num
ret.unitName = '元'
ret.unit = 1
break
}
// ret.num = fillNumber(ret.num,2);
// alert(ret.num);
return ret
}
/**
* 获取某年的某天是第几周
* @param {Number} y
* @param {Number} m
* @param {Number} d
* @returns {Number}
*/
function getWeekNumber (date) {
var year = date.getFullYear()
var month = date.getMonth()
var days = date.getDate()
// 那一天是那一年中的第多少天
for (var i = 0; i < month; i++) {
days += getMonthDays(year, i)
}
// 那一年第一天是星期几
var yearFirstDay = new Date(year, 0, 1).getDay() || 7
var week = null
// if (yearFirstDay == 1) {
// week = Math.ceil(days / yearFirstDay)
// } else {
// days -= (7 - yearFirstDay + 1)
// week = Math.ceil(days / 7) + 1
// }
days -= (7 - yearFirstDay + 1)
week = Math.ceil(days / 7) + 1
return week
}
// js获取一年有多少周
function getNumOfWeeks (year) {
var d = new Date(year, 0, 1)
var yt = ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? 366 : 365
return Math.ceil((yt - d.getDay()) / 7.0)
}
/**
* 对字符串进行Unicode编码
* @param str
* @returns {string}
*/
function decodeStr (str) {
var res = []
for (var i = 0; i < str.length; i++)
res[i] = ('00' + str.charCodeAt(i).toString(16)).slice(-4);
return '\\u' + res.join('\\u')
}
/**
* 对unicode编码格式的字符串进行解码
* @param str
* @returns {string}
*/
function undecodeStr (str) {
var result = []
var strArr = str.split('\\u') //根据 \u 分割
var len = strArr.length
for (var i = 0; i < len; i++) {
if (strArr[i]) {
result.push(String.fromCharCode(parseInt(strArr[i], 16))) //16 转 10
}
}
return result.join('')
}
// 判断设备
function getDeviceType()
{
let sUserAgent = navigator.userAgent.toLowerCase();
if(/ipad/i.test(sUserAgent))
return "iPad";
if(/iphone/i.test(sUserAgent))
return "iPhone";
if(/android/i.test(sUserAgent))
return "Android";
if(/ipod/i.test(sUserAgent))
return "iPod";
if(/blackberry/i.test(sUserAgent))
return "BlackBerry";
if(/windows phone/i.test(sUserAgent))
return "Windows Phone";
return "PC";
}
var new_arr = [];
var arr=[1,22,44,55,3,1,2,2,4];
function uniqueArrayObj(data){
new_arr=[];
for(var i=0;i<data.length;i++) {
var items=data[i];
//判断元素是否存在于new_arr中,如果不存在则插入到new_ar中
if($.inArray(items,new_arr)==-1) {
new_arr.push(items);
}
}
return new_arr;
}
uniqueArrayObj(arr)