import moment from 'moment'
// unix时间戳格式器(默认年月日时分秒)
export function unixTimestampFilter (value, format = 'YYYY-MM-DD HH:mm:ss') {
if (moment(parseInt(value)).format(format) === 'Invalid date') {
return value
} else {
return moment(parseInt(value)).format(format)
}
}
// 四舍五入(默认到小数点后两位)格式数据
export function decimalNumFilter (value, num = 2) {
let _test = /^-?\d+(\.\d*)?$/.test(value)
if (_test) {
const _number = parseFloat(value)
return _number.toFixed(num)
} else if (typeof value === 'number') { // 处理极小数 如:2.220446049250313e-16
return value.toFixed(2)
}
return value
}
// 是否
export function isTrue (value) {
switch (value) {
case 1 : return '是'
case '1' : return '是'
case 'true' : return '是'
case true : return '是'
case 0 : return '否'
case '0' : return '否'
case false : return '否'
case 'false' : return '否'
default : return '未知'
}
}
// 字典过滤
export function filterDataBaseDictionary (value) {
const DataBaseDictionary = dotData(window, 'DataBaseDictionary') || {}
return DataBaseDictionary[value] || value
}
// 天 小时 转化
export function filterSecond (value) {
if (String(value) === '0') {
return '0'
}
if (empty(value) || !isIntegerStringUtil(value)) {
return value
}
let temp = ''
/* const day = Math.floor(value / (24 * 60 * 60))
if (day) {
temp += `${day}天`
value -= day * 24 * 60 * 60
} */
const hour = Math.floor(value / (60 * 60))
if (hour) {
temp += `${hour}小时`
value -= hour * 60 * 60
}
const minute = Math.floor(value / 60)
if (minute) {
temp += `${minute}分钟`
}
if (temp === '') {
temp = '小于1分钟'
}
return temp
}
// 数字金额小写转换成中文大写
export function changeChineseNumber (num) {
num = decimalNumFilter(num)
let pre = ''
if (isNaN(num) || num > Number.MAX_VALUE) return ''
if (num < 0) {
num = Math.abs(num)
pre = '负'
}
let cn = '零壹贰叁肆伍陆柒捌玖'
let unit = ['拾佰仟', '分角']
let unit1 = ['万亿', '']
let numArray = num.toString().split('.')
// let start = new Array(numArray[0].length - 1, 2)
let start = [numArray[0].length - 1, 2]
function toChinese (num, index) {
let num2 = num.replace(/\d/g, function ($1) {
return cn.charAt($1) + unit[index].charAt(start-- % 4 ? start % 4 : -1)
})
return num2
}
for (let i = 0; i < numArray.length; i++) {
let tmp = ''
for (let j = 0; j * 4 < numArray[i].length; j++) {
let strIndex = numArray[i].length - (j + 1) * 4
let str = numArray[i].substring(strIndex, strIndex + 4)
start = i ? 2 : str.length - 1
let tmp1 = toChinese(str, i)
tmp1 = tmp1.replace(/(零.)+/g, '零').replace(/零+$/, '')
tmp1 = tmp1.replace(/^壹拾/, '拾')
tmp = (tmp1 + unit1[i].charAt(j - 1)) + tmp
}
numArray[i] = tmp
}
numArray[1] = numArray[1] ? numArray[1] : ''
numArray[0] = numArray[0] ? numArray[0] + '元' : [numArray[0], numArray[1] = numArray[1].replace(/^零+/, '')]
numArray[1] = numArray[1].match(/分/) ? numArray[1] : numArray[1] + '整'
return pre + numArray[0] + numArray[1]
}
封装:项目中常用的过滤器---时间日期过滤器、四舍五入过滤器、字典过滤器
猜你喜欢
转载自blog.csdn.net/qq_37126704/article/details/86699276
今日推荐
周排行