fix point rule unit

This commit is contained in:
Timi
2026-08-19 15:46:03 +08:00
parent ff156926f5
commit 84bc6d137d
5 changed files with 150 additions and 103 deletions
+18 -5
View File
@@ -54,13 +54,13 @@ export function formatEventNumberValue(event: EventItem | undefined, value?: num
return `${value}${event?.valueUnit === "COUNT" ? " 次" : ""}`;
}
const centText = String(Math.trunc(value));
const yuanText = centText.length < 3 ? "0" : centText.slice(0, -2);
const decimalText = centText.slice(-2).padStart(2, "0").replace(/0+$/, "");
return `${yuanText}${decimalText ? `.${decimalText}` : ""} 元`;
return `${formatCentValue(value)} 元`;
}
function resolveCentValue(valueText: string): number | undefined {
/**
* 将金额字符串转换为分,最多保留两位小数。
*/
export function resolveCentValue(valueText: string): number | undefined {
const match = /^(\d+)(?:\.(\d{0,2}))?$/.exec(valueText);
if (!match) {
return undefined;
@@ -74,3 +74,16 @@ function resolveCentValue(valueText: string): number | undefined {
}
return Number(centValue);
}
/**
* 将整数分格式化为不带单位的元字符串。
*/
export function formatCentValue(value: number): string {
if (!Number.isSafeInteger(value) || value < 0) {
return "";
}
const centText = String(value);
const yuanText = centText.length < 3 ? "0" : centText.slice(0, -2);
const decimalText = centText.slice(-2).padStart(2, "0").replace(/0+$/, "");
return `${yuanText}${decimalText ? `.${decimalText}` : ""}`;
}