From 7819b4f8a29ab1135aa1ed50159ac129bc0c9dab Mon Sep 17 00:00:00 2001 From: zdl <3489966805@qq.com> Date: Thu, 11 Dec 2025 10:58:52 +0800 Subject: [PATCH] =?UTF-8?q?feat(utils):=20=E6=B7=BB=E5=8A=A0=E6=B7=B1?= =?UTF-8?q?=E5=BA=A6=E5=88=86=E6=9E=90=E6=A0=BC=E5=BC=8F=E5=8C=96=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - formatCurrency: 货币格式化(支持亿/万单位) - formatBusinessRevenue: 营收格式化(智能单位转换) - formatPercentage: 百分比格式化 从 DeepAnalysisTab 提取合并到全局工具库 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- src/utils/priceFormatters.js | 68 ++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/src/utils/priceFormatters.js b/src/utils/priceFormatters.js index e93d4dfb..b4642867 100644 --- a/src/utils/priceFormatters.js +++ b/src/utils/priceFormatters.js @@ -103,3 +103,71 @@ export const PriceArrow = ({ value }) => { return ; }; + +// ==================== 货币/数值格式化 ==================== + +/** + * 格式化货币金额(自动选择单位:亿元/万元/元) + * @param {number|null|undefined} value - 金额(单位:元) + * @returns {string} 格式化后的金额字符串 + */ +export const formatCurrency = (value) => { + if (value === null || value === undefined) return '-'; + const absValue = Math.abs(value); + if (absValue >= 100000000) { + return (value / 100000000).toFixed(2) + '亿元'; + } else if (absValue >= 10000) { + return (value / 10000).toFixed(2) + '万元'; + } + return value.toFixed(2) + '元'; +}; + +/** + * 格式化业务营收(支持指定单位) + * @param {number|null|undefined} value - 营收金额 + * @param {string} [unit] - 原始单位(元/万元/亿元) + * @returns {string} 格式化后的营收字符串 + */ +export const formatBusinessRevenue = (value, unit) => { + if (value === null || value === undefined) return '-'; + if (unit) { + if (unit === '元') { + const absValue = Math.abs(value); + if (absValue >= 100000000) { + return (value / 100000000).toFixed(2) + '亿元'; + } else if (absValue >= 10000) { + return (value / 10000).toFixed(2) + '万元'; + } + return value.toFixed(0) + '元'; + } else if (unit === '万元') { + const absValue = Math.abs(value); + if (absValue >= 10000) { + return (value / 10000).toFixed(2) + '亿元'; + } + return value.toFixed(2) + '万元'; + } else if (unit === '亿元') { + return value.toFixed(2) + '亿元'; + } else { + return value.toFixed(2) + unit; + } + } + // 无单位时,假设为元 + const absValue = Math.abs(value); + if (absValue >= 100000000) { + return (value / 100000000).toFixed(2) + '亿元'; + } else if (absValue >= 10000) { + return (value / 10000).toFixed(2) + '万元'; + } + return value.toFixed(2) + '元'; +}; + +/** + * 格式化百分比 + * @param {number|null|undefined} value - 百分比值 + * @param {number} [decimals=2] - 小数位数 + * @returns {string} 格式化后的百分比字符串 + */ +export const formatPercentage = (value, decimals = 2) => { + if (value === null || value === undefined) return '-'; + return value.toFixed(decimals) + '%'; +};