cloudNew/src/utils/publicMethods.ts

25 lines
702 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 封装的一些公共方法
// 保留两位小数 不满两位小数的用0补齐 有千分号
export const handleFormatNumber = (num: any) => {
// 确保 num 是数字类型
const parsedNum = Number(num);
// 如果转换后仍然是 NaN返回原始值
if (isNaN(parsedNum)) return "NaN";
// 如果输入为 0直接返回 '0.00'
if (parsedNum === 0) return "0.00";
// 先处理千分号格式,保留两位小数
let [integer, decimal] = parsedNum.toFixed(2).split("."); // 保留两位小数
// 处理千分号
integer = integer.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
// 返回格式化后的数字
return `${integer}.${decimal}`;
};