From 15cad84b03d10eee42046db7545dbae384424fa4 Mon Sep 17 00:00:00 2001 From: stmSi Date: Thu, 25 Jul 2024 13:23:20 +0700 Subject: [PATCH] fix: issue where negative values like -0.0x were not handled correctly --- .../builtin-ledger-grpc-svc/src/domain/converters.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/builtin-ledger-grpc-svc/src/domain/converters.ts b/packages/builtin-ledger-grpc-svc/src/domain/converters.ts index feedfbb..a0005f1 100644 --- a/packages/builtin-ledger-grpc-svc/src/domain/converters.ts +++ b/packages/builtin-ledger-grpc-svc/src/domain/converters.ts @@ -59,8 +59,10 @@ export function bigintToString(bigintValue: bigint, decimals: number): string { } decimals = decimals || 0; + const isNegative = bigintValue < 0n; + let bigintValueToString: string = bigintValue.toString().replace("-", ""); + // Get the string corresponding to the bigint and insert a dot according to the decimals. - let bigintValueToString: string = bigintValue.toString(); if (bigintValueToString.length <= decimals) { bigintValueToString = "0".repeat(decimals - bigintValueToString.length + 1) + bigintValueToString; } @@ -79,5 +81,10 @@ export function bigintToString(bigintValue: bigint, decimals: number): string { finalString = finalString.slice(0, -1); } + // Handle negative numbers + if (isNegative) { + finalString = "-" + finalString; + } + return finalString; }