适配
This commit is contained in:
51
src/utils.ts
51
src/utils.ts
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user