Commit 15b0b7d9 by 张立顺

feat 功能开发

1 parent cb6fa1f4
...@@ -401,25 +401,25 @@ Page({ ...@@ -401,25 +401,25 @@ Page({
this.setGoodsTotal(goodsList); this.setGoodsTotal(goodsList);
}, },
removeLeadingZerosAndEnsureNonNegative(inputStr) { // removeLeadingZerosAndEnsureNonNegative(inputStr) {
// 使用正则表达式去掉前导零 // // 使用正则表达式去掉前导零
let trimmedStr = inputStr.replace(/^0+/, ""); // let trimmedStr = inputStr.replace(/^0+/, "");
let [number, decimal] = trimmedStr.split("."); // let [number, decimal] = trimmedStr.split(".");
if (decimal != undefined) { // if (decimal != undefined) {
decimal = decimal.substring(0, 2); // decimal = decimal.substring(0, 2);
number += `.${decimal}`; // number += `.${decimal}`;
trimmedStr = number; // trimmedStr = number;
} // }
// 将字符串转换为浮点数 // // 将字符串转换为浮点数
const numberValue = parseFloat(trimmedStr); // const numberValue = parseFloat(trimmedStr);
// 如果转换后的值为 NaN 或者小于 0,则默认为 0 // // 如果转换后的值为 NaN 或者小于 0,则默认为 0
if (isNaN(numberValue) || numberValue < 0) { // if (isNaN(numberValue) || numberValue < 0) {
return 0; // return 0;
} // }
return trimmedStr; // return trimmedStr;
}, // },
priceBlur(e) { priceBlur(e) {
let { index, item } = e.currentTarget.dataset; let { index, item } = e.currentTarget.dataset;
let target = `goodsList[${index}].price`; let target = `goodsList[${index}].price`;
......
...@@ -161,23 +161,23 @@ const removeLeadingZerosAndEnsureNonNegative = inputStr => { ...@@ -161,23 +161,23 @@ const removeLeadingZerosAndEnsureNonNegative = inputStr => {
// 使用正则表达式过滤掉除数字和小数点以外的字符 // 使用正则表达式过滤掉除数字和小数点以外的字符
const filteredStr = inputStr.replace(/[^0-9.]/g, ''); const filteredStr = inputStr.replace(/[^0-9.]/g, '');
// 使用正则表达式去掉前导零,但保留一个零作为整数部分 // 使用正则表达式去掉前导零,但保留一个零作为整数部分
let trimmedStr = filteredStr.replace(/^0+(?=\d)|^0+(?=\.)/, ''); let trimmedStr = filteredStr.replace(/^0+(?=\d)|^0+(?=\.)/, '');
// 如果字符串以小数点结尾,不做任何修改 // 如果字符串以小数点结尾,不做任何修改
if (trimmedStr === '.') { if (trimmedStr === '.') {
return '0.'; return '0.';
} }
// 如果字符串以小数点开始,添加一个零作为整数部分 // 如果字符串以小数点开始,添加一个零作为整数部分
if (trimmedStr.startsWith('.')) { if (trimmedStr.startsWith('.')) {
trimmedStr = '0' + trimmedStr; trimmedStr = '0' + trimmedStr;
} }
// // 如果小数点后没有数字,则在末尾添加一个零 // // 如果小数点后没有数字,则在末尾添加一个零
// if (trimmedStr.indexOf('.') !== -1 && trimmedStr.endsWith('.')) { // if (trimmedStr.indexOf('.') !== -1 && trimmedStr.endsWith('.')) {
// trimmedStr += '0'; // trimmedStr += '0';
// } // }
// // 如果字符串以小数点结尾,不做任何修改 // // 如果字符串以小数点结尾,不做任何修改
// if (trimmedStr.endsWith('.')) { // if (trimmedStr.endsWith('.')) {
// return trimmedStr; // return trimmedStr;
...@@ -196,6 +196,10 @@ const removeLeadingZerosAndEnsureNonNegative = inputStr => { ...@@ -196,6 +196,10 @@ const removeLeadingZerosAndEnsureNonNegative = inputStr => {
if (isNaN(numberValue) || numberValue < 0) { if (isNaN(numberValue) || numberValue < 0) {
return 0; return 0;
} }
// 如果转换后的值大于于 100000,则默认为 100000
if (isNaN(numberValue) || numberValue > 100000) {
return 100000;
}
return trimmedStr; return trimmedStr;
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!