1.数据格式化,数字超出三位数 逗号 分割: 123,23
/**
* 格式化数字,超过3个加 逗号 分割
* @returns 123,456
*/
function intervalNum(data: number | string) {
if (data === undefined) {
return
}
if (typeof data === 'number') {
data = data.toFixed(2)
}
data = data.replace('.00', '')
data = data.substring(data.length - 2) === '.0' ? data.replace('.0', '') : data
// 小于3位数,直接返回
if (data < '1000') {
return data
}
if (!(data.split('.')[0].length > 3)) {
return data
}
let price = data.split('.')[0]
const pricePoint = data.split('.')[1]
let result = ''
price = price.toString()
result =
price.length > 6
? `${price.substring(0, price.length - 6)},${price.substring(
price.length - 6,
price.length - 3
)},${price.substring(price.length - 3, price.length)}`
: `${price.substring(0, price.length - 3)},${price.substring(price.length - 3, price.length)}`
result = pricePoint ? `${result},${pricePoint}` : result
return result
}
2.枚举键值互换
/**
* convertEnum
* 枚举键值互换 title:'你好' => '你好':title
*/
export const convertKeyValueEnum = (obj) => {
const result = {}
if (typeof obj === 'object') {
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
result[obj[key]] = key
}
}
}
return result
}