From 0c6e642a3366bf28b31f2f48768a07138af7ee59 Mon Sep 17 00:00:00 2001 From: tone Date: Fri, 9 Jan 2026 11:15:15 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20parseAmountToInt=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=B0=BE=E9=9A=8F0=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tonesc-red-packet/lib/utils.ts | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/tonesc-red-packet/lib/utils.ts b/tonesc-red-packet/lib/utils.ts index 8acf3b9..106d884 100644 --- a/tonesc-red-packet/lib/utils.ts +++ b/tonesc-red-packet/lib/utils.ts @@ -9,16 +9,27 @@ export function parseAmountToInt(amountStr: string, precision: number): bigint { // 例:"123.45", precision=2 → 12345n // 例:"0.00001234", precision=8 → 1234n - const [intPart, fracPart = ""] = amountStr.split(".") + // 去掉正负号处理(如不需要可删) + const negative = amountStr.startsWith("-") + const normalizedStr = negative ? amountStr.slice(1) : amountStr - if (fracPart.length > precision) { - throw new Error(`Amount ${amountStr} exceeds precision ${precision}`) + const [intPart, fracRaw = ""] = normalizedStr.split(".") + + // 关键修复点:允许尾随 0 + const fracTrimmed = fracRaw.replace(/0+$/, "") + + if (fracTrimmed.length > precision) { + throw new Error( + `Amount ${amountStr} exceeds allowed precision ${precision}` + ) } - const paddedFrac = fracPart.padEnd(precision, "0") - const normalized = intPart + paddedFrac + const paddedFrac = fracTrimmed.padEnd(precision, "0") + const combined = intPart + paddedFrac - return BigInt(normalized) + const value = BigInt(combined) + + return negative ? -value : value } export function formatAmountFromInt(value: bigint, precision: number): string {