xiugai
This commit is contained in:
@@ -89,29 +89,42 @@ export default {
|
||||
},
|
||||
methods: {
|
||||
// 解析rate字符串,提取百分比数值
|
||||
parseRateString(rateStr) {
|
||||
if (!rateStr || typeof rateStr !== 'string') {
|
||||
// 改进的 parseRateString 方法:能处理数字和字符串类型
|
||||
parseRateString(rateValue) {
|
||||
// 如果是 undefined 或 null,返回默认值
|
||||
if (rateValue === undefined || rateValue === null) {
|
||||
return { displayText: '0%', progressValue: 0 };
|
||||
}
|
||||
|
||||
// 尝试匹配百分比数字,如"减亏93%"中的93
|
||||
const match = rateStr.match(/(\d+(\.\d+)?)%/);
|
||||
if (match) {
|
||||
const percentValue = parseFloat(match[1]);
|
||||
// 如果是数字类型,直接处理
|
||||
if (typeof rateValue === 'number') {
|
||||
return {
|
||||
displayText: rateStr, // 保持原字符串显示
|
||||
progressValue: percentValue // 提取的百分比数值用于进度条
|
||||
displayText: `${rateValue.toFixed(2)}%`,
|
||||
progressValue: Math.min(Math.max(rateValue, 0), 100)
|
||||
};
|
||||
}
|
||||
|
||||
// 如果没有匹配到百分比,尝试解析纯数字
|
||||
const numMatch = rateStr.match(/\d+(\.\d+)?/);
|
||||
if (numMatch) {
|
||||
const numValue = parseFloat(numMatch[0]);
|
||||
return {
|
||||
displayText: rateStr,
|
||||
progressValue: numValue
|
||||
};
|
||||
// 如果是字符串类型,使用正则表达式处理
|
||||
if (typeof rateValue === 'string') {
|
||||
// 尝试匹配百分比数字,如"减亏93%"中的93
|
||||
const match = rateValue.match(/(\d+(\.\d+)?)%/);
|
||||
if (match) {
|
||||
const percentValue = parseFloat(match[1]);
|
||||
return {
|
||||
displayText: rateValue,
|
||||
progressValue: Math.min(Math.max(percentValue, 0), 100)
|
||||
};
|
||||
}
|
||||
|
||||
// 如果没有匹配到百分比,尝试解析纯数字
|
||||
const numMatch = rateValue.match(/\d+(\.\d+)?/);
|
||||
if (numMatch) {
|
||||
const numValue = parseFloat(numMatch[0]);
|
||||
return {
|
||||
displayText: rateValue,
|
||||
progressValue: Math.min(Math.max(numValue, 0), 100)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 默认返回
|
||||
|
||||
Reference in New Issue
Block a user