format amount split

This commit is contained in:
Timi
2026-09-08 15:21:09 +08:00
parent cbc8197057
commit 74127fc20f
12 changed files with 78 additions and 40 deletions
+2 -4
View File
@@ -1,4 +1,5 @@
import type { EventItem } from "@/types/EventItem";
import { formatCentsAmount } from "@/utils/money";
/**
* 返回事件数值单位的前端文案。
@@ -82,8 +83,5 @@ 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}` : ""}`;
return formatCentsAmount(value).replace(/\.00$/, "").replace(/(\.\d)0$/, "$1");
}
+36
View File
@@ -0,0 +1,36 @@
/**
* 将整数部分按每 4 位分组,适用于中文金额展示。
* 例如:999999.99 -> 99,9999.99。
*/
export function formatWanInteger(value: number | string): string {
const text = String(value).trim();
const negative = text.startsWith("-");
const integerText = (negative || text.startsWith("+")) ? text.slice(1) : text;
const normalizedText = integerText.replace(/^0+(?=\d)/, "") || "0";
const groupedText = normalizedText.replace(/\B(?=(\d{4})+(?!\d))/g, ",");
return `${negative ? "-" : ""}${groupedText}`;
}
/** 将整数分转换为带两位小数的金额展示文本。 */
export function formatCentsAmount(cents?: number | null): string {
if (cents === undefined || cents === null || !Number.isFinite(cents)) {
return "未设置";
}
const negative = cents < 0;
const absolute = Math.abs(Math.trunc(cents));
const yuan = Math.floor(absolute / 100);
const fen = String(absolute % 100).padStart(2, "0");
return `${negative ? "-" : ""}${formatWanInteger(yuan)}.${fen}`;
}
/** 将元金额转换为按每 4 位分组的展示文本,最多保留两位小数。 */
export function formatYuanAmount(value?: number | null): string {
if (value === undefined || value === null || !Number.isFinite(value)) {
return "0";
}
const [integerText, fractionText = ""] = value.toFixed(2).split(".");
const normalizedFractionText = fractionText.replace(/0+$/, "");
return `${formatWanInteger(integerText)}${normalizedFractionText ? `.${normalizedFractionText}` : ""}`;
}
+2 -8
View File
@@ -1,5 +1,6 @@
import type { PackageConfigStatus } from "@/types/Package";
import type { FundOperationType, FundRecordStatus } from "@/types/SalesOrder";
import { formatCentsAmount } from "@/utils/money";
/** 生成套餐业务幂等请求号。每次新操作必须重新生成。 */
export function createPackageRequestNo(): string {
@@ -8,14 +9,7 @@ export function createPackageRequestNo(): string {
/** 将分转换为金额展示文本,不用浮点数做金额格式化。 */
export function formatPackageAmount(cents?: number | null): string {
if (cents === undefined || cents === null) {
return "未设置";
}
const negative = cents < 0;
const absolute = Math.abs(Math.trunc(cents));
const yuan = Math.floor(absolute / 100);
const fen = String(absolute % 100).padStart(2, "0");
return `${negative ? "-" : ""}${yuan}.${fen}`;
return formatCentsAmount(cents);
}
/** 将分转换为金额输入框文本。 */
+2 -7
View File
@@ -1,5 +1,6 @@
import type { Product, ProductCategoryStatus, ProductSku, ProductSpec, ProductSpecValue, ProductStatus, ProductType, StockOperationType } from "@/types/Product";
import { getAttachmentReadURL } from "@/utils/attachment.ts";
import { formatCentsAmount } from "@/utils/money";
/** 生成产品域写操作的幂等请求号。一次失败重试应复用调用方已经生成的请求号。 */
export function createProductRequestNo(): string {
@@ -8,13 +9,7 @@ export function createProductRequestNo(): string {
/** 将分转换成金额展示文本。金额不使用浮点数格式化。 */
export function formatProductAmount(cents?: number | null): string {
if (cents === undefined || cents === null) {
return "未设置";
}
const negative = cents < 0;
const absolute = Math.abs(Math.trunc(cents));
return `${negative ? "-" : ""}${Math.floor(absolute / 100)}.${String(absolute % 100).padStart(2, "0")}`;
return formatCentsAmount(cents);
}
/** 将元金额输入精确转换为分。 */