62 lines
1.1 KiB
TypeScript
62 lines
1.1 KiB
TypeScript
/**
|
|
* 计算百分比变化
|
|
* @param a
|
|
* @param b
|
|
*/
|
|
export const calculatePercentageChange = (cur: number, prev: number) => {
|
|
// 计算差值
|
|
const diff = cur - prev
|
|
|
|
if (diff === 0)
|
|
return {
|
|
diff,
|
|
percentage: "0",
|
|
}
|
|
|
|
// 计算百分比
|
|
const percentage = Math.abs((diff / prev) * 100).toFixed(1)
|
|
|
|
return {
|
|
diff,
|
|
percentage,
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 计算周同比
|
|
* @param cur
|
|
* @param prev
|
|
* @param needCN
|
|
*/
|
|
export const calculateWeeklyRate = (
|
|
cur: string | number,
|
|
prev: string | number,
|
|
needCN = true
|
|
) => {
|
|
const { diff, percentage } = calculatePercentageChange(
|
|
Number(cur),
|
|
Number(prev)
|
|
)
|
|
if (diff > 0)
|
|
return {
|
|
text: `<font color='red'>${
|
|
needCN ? "较上周 " : ""
|
|
}↑${percentage}%</font>`,
|
|
diff,
|
|
percentage,
|
|
}
|
|
if (diff < 0)
|
|
return {
|
|
text: `<font color='green'>${
|
|
needCN ? "较上周 " : ""
|
|
}↓${percentage}%</font>`,
|
|
diff,
|
|
percentage,
|
|
}
|
|
return {
|
|
text: `<font color='gray'>${needCN ? "较上周 " : ""}0%</font>`,
|
|
diff,
|
|
percentage,
|
|
}
|
|
}
|