This commit is contained in:
iqudoo
2026-06-12 21:17:24 +08:00
parent 239e2eeebd
commit 220841444a
7 changed files with 240 additions and 81 deletions

View File

@@ -6,6 +6,7 @@ import {
DataSourceMeta,
TemplateElement,
TextFormat,
NumberPartDisplay,
ExportRangeConfig,
CutLineConfig,
VisibilityRule,
@@ -643,6 +644,51 @@ export function formatDisplayValue(
}
}
/** 按整数/小数部分拆分数值显示(如价格 19.9 →「19」与「.9」分元素展示) */
export function applyNumberPartDisplay(
value: string,
format: Exclude<TextFormat, 'text'>,
decimalPlaces: number | undefined,
part: NumberPartDisplay
): string {
if (part === 'full') {
return formatDisplayValue(value, format, decimalPlaces);
}
const trimmed = value.trim().replace(/,/g, '');
if (!trimmed) return '';
const num = Number(trimmed);
if (Number.isNaN(num)) {
return formatDisplayValue(value, format, decimalPlaces);
}
const places = decimalPlaces ?? 2;
const sign = num < 0 ? '-' : '';
const abs = Math.abs(num);
const intVal = Math.trunc(abs);
const intStr = intVal.toLocaleString('zh-CN');
if (part === 'integer') {
switch (format) {
case 'number':
return `${sign}${intStr}`;
case 'percent':
return `${sign}${intStr}%`;
case 'currency':
return `${sign}¥${intStr}`;
default:
return `${sign}${intStr}`;
}
}
const fracNum = abs - intVal;
let fracStr = fracNum.toFixed(places).slice(1);
if (!fracStr || fracStr === '.' || fracStr === '.' + '0'.repeat(places)) {
fracStr = places > 0 ? '.' + '0'.repeat(places) : '';
}
return fracStr;
}
/** 读取变量原始值(仅行数据;设计模式可选用默认值) */
export function getVariableRawValue(
varName: string,
@@ -693,10 +739,11 @@ export function resolveElementValue(
const formatSubst =
elem.type === 'text' && elem.textFormat && elem.textFormat !== 'text'
? (raw: string) =>
formatDisplayValue(
applyNumberPartDisplay(
raw,
elem.textFormat as Exclude<TextFormat, 'text'>,
elem.decimalPlaces
elem.decimalPlaces,
elem.numberPartDisplay ?? 'full'
)
: undefined;