utils_format.js
3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
/**
* 时间戳转化为年 月 日 时 分 秒
* ts: 传入时间戳
* format:返回格式,支持自定义,但参数必须与formateArr里保持一致
*/
const tsFormatTime = (timestamp, format) => {
format = format || 'Y-M-D h:m:s'
const formateArr = ['Y', 'M', 'D', 'h', 'm', 's']
let returnArr = []
let date = new Date(timestamp)
let year = date.getFullYear()
let month = date.getMonth() + 1
let day = date.getDate()
let hour = date.getHours()
let minute = date.getMinutes()
let second = date.getSeconds()
returnArr.push(year, month, day, hour, minute, second)
returnArr = returnArr.map(formatNumber)
for (var i in returnArr) {
format = format.replace(formateArr[i], returnArr[i])
}
return format
}
/**num:要转换的数值,fixed:精确几位小数 */
const decimal = (num, fixed) => {
const pow = Math.pow(10, fixed)
return Math.round(num * pow) / pow
}
const setPrice = price => {
let [num, decimal] = price.toString().split('.')
if (!decimal) decimal = '00'
return (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') + `.${decimal}`
}
// 格式化日期
const dateFormatter = str => {
let o = new Date()
if (str) {
o = new Date(str)
}
let y = o.getFullYear()
let m = o.getMonth() + 1
let d = o.getDate()
let time = y + '年' + m + '月' + d + '日'
return time
}
/**
* 大数字转换,将大额数字转换为万、千万、亿等
* @param value 数字值
*/
const bigNumberTransform = value => {
const newValue = ['', '', '']
let fr = 1000
let num = 3
let text1 = ''
let fm = 1
while (value / fr >= 1) {
fr *= 10
num += 1
// console.log('数字', value / fr, 'num:', num)
}
if (num <= 4) {
// 千
newValue[0] = parseInt(value / 1000) + ''
newValue[1] = '千'
} else if (num <= 8) {
// 万
text1 = parseInt(num - 4) / 3 > 1 ? '千万' : '万'
// tslint:disable-next-line:no-shadowed-variable
fm = text1 === '万' ? 10000 : 10000000
if (value % fm === 0) {
newValue[0] = parseInt(value / fm) + ''
} else {
newValue[0] = parseFloat(value / fm).toFixed(2) + ''
}
newValue[1] = text1
} else if (num <= 16) {
// 亿
text1 = (num - 8) / 3 > 1 ? '千亿' : '亿'
text1 = (num - 8) / 4 > 1 ? '万亿' : text1
text1 = (num - 8) / 7 > 1 ? '千万亿' : text1
// tslint:disable-next-line:no-shadowed-variable
fm = 1
if (text1 === '亿') {
fm = 100000000
} else if (text1 === '千亿') {
fm = 100000000000
} else if (text1 === '万亿') {
fm = 1000000000000
} else if (text1 === '千万亿') {
fm = 1000000000000000
}
if (value % fm === 0) {
newValue[0] = parseInt(value / fm) + ''
} else {
newValue[0] = parseFloat(value / fm).toFixed(2) + ''
}
newValue[1] = text1
}
if (value < 1000) {
newValue[0] = value + ''
newValue[1] = ''
}
return newValue.join('')
}
const numberFixedDigit = num => {
// 固定两位小数
num = num.match(/^\d*(\.?\d{0,2})/g)[0] || ''
return num
// this.$nextTick(() => {
// this.number = e.target.value
// })
}
const format = {
decimal,
setPrice,
dateFormatter,
bigNumberTransform,
tsFormatTime,
numberFixedDigit
}
export default format