/**
* 格式化数字
* @method formatNumber
* @param {num} 需要格式化的数字
* @param {decimalPlaces} 小数位数 默认2
* @return {num} 格式化后的数字
*/
export const formatNumber = (num, decimalPlaces = 2) => {
if(!num) return 0
// 确保 decimalPlaces 是一个有效的数字
decimalPlaces = Math.max(0, Math.floor(decimalPlaces))
// 将 num 转换为浮点数
const number = parseFloat(num)
// 检查是否是有效数字
if (isNaN(number)) return NaN // 返回 NaN 表示无效的输入
// 使用 toFixed() 保留指定的小数位数
const fixedNumber = number.toFixed(decimalPlaces)
// 转换为数字并去掉尾部的0
return parseFloat(fixedNumber)
}
12-20
1959