42 lines
905 B
JavaScript
42 lines
905 B
JavaScript
export function formatMoney(cent) {
|
|
if (!cent && cent !== 0) return '¥0'
|
|
const yuan = cent / 100
|
|
return `¥${yuan.toFixed(yuan % 1 === 0 ? 0 : 2)}`
|
|
}
|
|
|
|
export function formatPercent(value, decimals = 0) {
|
|
if (!value && value !== 0) return '0%'
|
|
return `${(value * 100).toFixed(decimals)}%`
|
|
}
|
|
|
|
export function formatNumber(num) {
|
|
if (!num && num !== 0) return '0'
|
|
return num.toLocaleString()
|
|
}
|
|
|
|
export function formatChange(current, previous) {
|
|
if (!previous) return { text: '', class: '' }
|
|
|
|
const diff = current - previous
|
|
const percent = Math.round((diff / previous) * 100)
|
|
|
|
if (diff < 0) {
|
|
return {
|
|
text: `较昨日 ${diff}`,
|
|
class: 'change-down',
|
|
percent: `${percent}%`
|
|
}
|
|
} else if (diff > 0) {
|
|
return {
|
|
text: `较昨日 +${diff}`,
|
|
class: 'change-up',
|
|
percent: `+${percent}%`
|
|
}
|
|
}
|
|
return {
|
|
text: '与昨日持平',
|
|
class: 'change-same',
|
|
percent: '0%'
|
|
}
|
|
}
|