页面优化
This commit is contained in:
@@ -406,24 +406,23 @@ function getUid(prefix) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化价格数额为字符串
|
||||
* 可对小数部分进行填充,默认不填充
|
||||
* @param price 价格数额,以分为单位!
|
||||
* @param fill 是否填充小数部分 0-不填充 1-填充第一位小数 2-填充两位小数
|
||||
* 格式化金额为字符串,单位为元
|
||||
* @param price 金额,单位元
|
||||
* @param fill 小数位数 0-不填充 1-一位 2-两位
|
||||
*/
|
||||
function priceFormat(price, fill = 0) {
|
||||
if (isNaN(price) || price === null || price === Infinity) {
|
||||
return price;
|
||||
}
|
||||
|
||||
let priceFormatValue = Math.round(parseFloat(`${price}`) * 10 ** 8) / 10 ** 8; // 恢复精度丢失
|
||||
priceFormatValue = `${Math.ceil(priceFormatValue) / 100}`; // 向上取整,单位转换为元,转换为字符串
|
||||
const rounded = Math.round(parseFloat(`${price}`) * 100) / 100;
|
||||
let priceFormatValue = `${rounded}`;
|
||||
if (fill > 0) {
|
||||
// 补充小数位数
|
||||
if (priceFormatValue.indexOf('.') === -1) {
|
||||
priceFormatValue = `${priceFormatValue}.`;
|
||||
}
|
||||
const n = fill - priceFormatValue.split('.')[1]?.length;
|
||||
const decimal = priceFormatValue.split('.')[1] || '';
|
||||
const n = fill - decimal.length;
|
||||
for (let i = 0; i < n; i++) {
|
||||
priceFormatValue = `${priceFormatValue}0`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user