cashier_app/commons/utils/format.js

55 lines
1.8 KiB
JavaScript
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.

/**
* 格式化价格函数,将价格限定在指定的最小值和最大值范围内,并保留两位小数。
*
* @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,isRerturnNullString=false) => {
if(price === undefined || price === null||price===''){
return isRerturnNullString?'':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;
}