商品管理-商品列表增加商品时对价格金额数量进行限制和数字格式化

This commit is contained in:
2024-07-25 11:32:37 +08:00
parent 54d06b72fe
commit 016ebf7b62
2 changed files with 48 additions and 16 deletions

25
src/utils/format.js Normal file
View File

@@ -0,0 +1,25 @@
/**
* 格式化价格函数,将价格限定在指定的最小值和最大值范围内,并保留两位小数。
*
* @param {number} price - 需要格式化的价格。
* @param {number} min - 价格的最小值。
* @param {number} max - 价格的最大值默认为100000000。
* @returns {number} - 返回格式化后的价格,如果超出范围则返回最小值或最大值。
*/
export const formatPrice = (price,min=-Infinity, max = 100000000 ) => {
if(price === undefined || price === null||price===''){
return 0
}
// 将价格转换为浮点数并保留两位小数
const newval = parseFloat(price.toFixed(2))
// 如果价格大于最大值,返回最大值
if (newval > max) {
return max
}
// 如果价格小于最小值,返回最小值
if (newval < min) {
return min
}
// 返回格式化后的价格
return newval
}