IPPSWAP-h5
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

150 lines
4.2 KiB

/**
* 格式化金额, 保留两位小数
* @param o
* @param o.amount {number | string} 金额
* @param o.precision {number} 精度, 默认 100 (分)
* @param o.thousand {boolean} 是否需要千分位, 默认 true
* @param o.decimal {boolean} 是否需要小数点, 默认 true
* @param o.decimalLength {number} 小数点位数, 默认 2
* @param o.decimalSeparator {string} 小数点分隔符, 默认 '.'
* @param o.thousandSeparator {string} 千分位分隔符, 默认 ','
* @return {string | number}
*/
export const amountFormat = (o) => {
if (o.amount === undefined || o.amount === null) {
return o.amount;
}
let amount = o.amount;
// 如果 amount 是字符串, 则转换为数字
if (typeof amount === 'string') {
amount = parseFloat(amount);
}
let precision = o.precision || 100;
let thousand = o.thousand === undefined ? true : o.thousand;
let decimal = o.decimal === undefined ? true : o.decimal;
let decimalLength = o.decimalLength || 2;
let decimalSeparator = o.decimalSeparator || '.';
let thousandSeparator = o.thousandSeparator || ',';
amount = amount / precision;
amount = amount.toFixed(decimalLength);
if (thousand) {
amount = amount.replace(/(\d)(?=(\d{3})+\.)/g, '$1' + thousandSeparator);
}
if (decimal) {
amount = amount.replace('.', decimalSeparator);
}
return amount;
}
/**
* 参数处理
* @param {*} params 参数
*/
export function tansParams(params) {
let result = ''
for (const propName of Object.keys(params)) {
const value = params[propName];
var part = encodeURIComponent(propName) + "=";
if (value !== null && value !== "" && typeof (value) !== "undefined") {
if (typeof value === 'object') {
for (const key of Object.keys(value)) {
if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
let params = propName + '[' + key + ']';
var subPart = encodeURIComponent(params) + "=";
result += subPart + encodeURIComponent(value[key]) + "&";
}
}
} else {
result += part + encodeURIComponent(value) + "&";
}
}
}
return result
}
// 精度计算乘法
export function NumberMul(arg1, arg2) {
let m = 0
const s1 = arg1.toString()
const s2 = arg2.toString()
try {
m += s1.split('.')[1].length
} catch (e) {}
try {
m += s2.split('.')[1].length
} catch (e) {}
return Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m)
}
// 精度计算除法
// 除数,被除数, 保留的小数点后的位数
export function NumberDiv(arg1, arg2) {
arg1 = parseFloat(arg1)
arg2 = parseFloat(arg2)
let t1 = 0
let t2 = 0
let r1; let r2
try {
t1 = arg1.toString().split('.')[1].length
} catch (e) {}
try {
t2 = arg2.toString().split('.')[1].length
} catch (e) {}
r1 = Number(arg1.toString().replace('.', ''))
r2 = Number(arg2.toString().replace('.', ''))
return Mul(r1 / r2, Math.pow(10, t2 - t1))
}
export function Mul(arg1, arg2) {
arg1 = parseFloat(arg1)
arg2 = parseFloat(arg2)
let m = 0
const s1 = arg1.toString()
const s2 = arg2.toString()
try {
m += s1.split('.')[1].length
} catch (e) {}
try {
m += s2.split('.')[1].length
} catch (e) {}
return Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m)
}
export const GetADateTime = {
// 获取今天起始时间日期
getTodayBegin() {
const date = new Date();
const year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
// 年月日时分秒
// 月和日如果是一位数的话,前面补0
if (month < 10) {
month = '0' + month;
}
if (day < 10) {
day = '0' + day;
}
return year + '-' + month + '-' + day + ' 00:00:00';
},
// 获取今天结束时间日期
getTodayEnd() {
const date = new Date();
const year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
// 年月日时分秒
// 月和日如果是一位数的话,前面补0
if (month < 10) {
month = '0' + month;
}
if (day < 10) {
day = '0' + day;
}
return year + '-' + month + '-' + day + ' 23:59:59';
}
}