index.wxs
2.29 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
/**设置千分位 */
var setPrice = function (n = 0.0) {
if ((typeof n == 'string' || typeof n == 'number') && !isNaN(n)) {
n = parseFloat(n).toFixed(2)
var r = getRegExp('(d{1,3})(?=(d{3})+(?:.))', 'g')
var p = n.replace(r, ',')
return p
// var r = getRegExp('/./', 'g')
// var p = n.replace(r, 'test')
// return n
} else {
console.log(n, '不是一个数字', !isNaN(n))
}
}
var threeNumForTwo = {
money_three_for_two_thousands: function (num) {
//num = parseInt(num);
// return num
var num = num + ''
var d = ''
if (num.slice(0, 1) == '-') {
d = num.slice(0, 1)
num = num.slice(1)
}
var len = num.length
var index = num.indexOf('.')
if (index == -1) {
num = num + '.00'
} else if (index + 2 == len) {
num = num + '0'
}
var index = num.indexOf('.') // 字符出现的位置
var num2 = num.slice(-3)
num = num.slice(0, index)
var result = ''
while (num.length > 3) {
result = ',' + num.slice(-3) + result
num = num.slice(0, num.length - 3)
}
if (num) {
result = num + result
}
// return result
return d + (result + num2)
}
}
var format = {
yearFormat: function (date) {
var _date = getDate(date)
var year = _date.getFullYear()
return year
},
dateFormat: function (date) {
var _date = getDate(date)
var year = _date.getFullYear()
var month = _date.getMonth() + 1
var day = _date.getDate()
return year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day)
},
mileageFormat: function (mileage) {
if (mileage > 10000) {
return (mileage / 10000).toFixed(2) + '万公里'
} else {
return mileage + '公里'
}
},
// 万单位,保留两位小数
priceFormat: function(price) {
return (price / 10000).toFixed(2)
},
// 万单位,加千位符
threeNumForTwoPriceFormat: function(num) {
return threeNumForTwo.money_three_for_two_thousands(format.priceFormat(num))
}
}
/*
* 导出
*/
module.exports = {
setPrice: setPrice,
money_three_for_two_thousands: threeNumForTwo.money_three_for_two_thousands, //暴露接口调用
threeNumForTwoPriceFormat: format.threeNumForTwoPriceFormat,
dateFormat: format.dateFormat,
mileageFormat: format.mileageFormat,
}