1
This commit is contained in:
44
Web/src/utils/format.js
Normal file
44
Web/src/utils/format.js
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* 格式化金额
|
||||
* @param {number} value 金额数值
|
||||
* @returns {string} 格式化后的金额字符串
|
||||
*/
|
||||
export const formatMoney = (value) => {
|
||||
if (!value && value !== 0) {
|
||||
return '0'
|
||||
}
|
||||
return Number(value)
|
||||
.toFixed(0)
|
||||
.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化日期
|
||||
* @param {Date|string} date 日期
|
||||
* @param {string} format 格式化模板
|
||||
* @returns {string} 格式化后的日期字符串
|
||||
*/
|
||||
export const formatDate = (date, format = 'YYYY-MM-DD') => {
|
||||
const d = new Date(date)
|
||||
const year = d.getFullYear()
|
||||
const month = String(d.getMonth() + 1).padStart(2, '0')
|
||||
const day = String(d.getDate()).padStart(2, '0')
|
||||
|
||||
return format
|
||||
.replace('YYYY', year)
|
||||
.replace('MM', month)
|
||||
.replace('DD', day)
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化百分比
|
||||
* @param {number} value 数值
|
||||
* @param {number} decimals 小数位数
|
||||
* @returns {string} 格式化后的百分比字符串
|
||||
*/
|
||||
export const formatPercent = (value, decimals = 1) => {
|
||||
if (!value && value !== 0) {
|
||||
return '0%'
|
||||
}
|
||||
return `${Number(value).toFixed(decimals)}%`
|
||||
}
|
||||
Reference in New Issue
Block a user