54 lines
1.8 KiB
JavaScript
54 lines
1.8 KiB
JavaScript
/**
|
||
* 格式化价格函数,将价格限定在指定的最小值和最大值范围内,并保留两位小数。
|
||
*
|
||
* @param {number} price - 需要格式化的价格。
|
||
* @param {number} min - 价格的最小值。
|
||
* @param {number} max - 价格的最大值,默认为100000000。
|
||
* @param {Boolean} returnIsArea - 是否返回值符合范围区间,默认为false。
|
||
* @returns {number} - 返回格式化后的价格,如果超出范围则返回最小值或最大值。
|
||
*/
|
||
export const formatPrice = (price,min=-Infinity, max = 100000000,returnIsArea=false ) => {
|
||
if(price === undefined || price === null||price===''){
|
||
return 0
|
||
}
|
||
// 将价格转换为浮点数并保留两位小数
|
||
const newval = parseFloat((Math.floor(price * 100) / 100).toFixed(2))
|
||
// 如果价格大于最大值,返回最大值
|
||
if (newval > max) {
|
||
return returnIsArea?{value:max,error:true}:max
|
||
}
|
||
// 如果价格小于最小值,返回最小值
|
||
if (newval < min) {
|
||
return returnIsArea?{value:min,error:true}:min
|
||
}
|
||
// 如果价格小于最小值,返回最小值
|
||
if (newval < min) {
|
||
return min
|
||
}
|
||
// 返回格式化后的价格
|
||
return newval
|
||
}
|
||
export function returnReverseVal(val, isReturnString = true) {
|
||
const isBol = typeof val === "boolean";
|
||
const isString = typeof val === "string";
|
||
let reverseNewval = "";
|
||
if (isBol) {
|
||
reverseNewval = !val;
|
||
}
|
||
if (isString) {
|
||
reverseNewval = val === "true" ? "false" : "true";
|
||
}
|
||
return reverseNewval;
|
||
}
|
||
export function returnBoolean(val) {
|
||
const isBol = typeof val === "boolean";
|
||
const isString = typeof val === "string";
|
||
let newval = "";
|
||
if (isBol) {
|
||
newval = val;
|
||
}
|
||
if (isString) {
|
||
newval = val === "true" ? true : false;
|
||
}
|
||
return newval;
|
||
} |