From 40fc624dffa7f767f30ca496444e60d032c57e7b Mon Sep 17 00:00:00 2001 From: jejolare Date: Wed, 13 Mar 2024 20:26:45 +0700 Subject: [PATCH] add E notation convertion --- src/pages/Transaction/Transaction.tsx | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/pages/Transaction/Transaction.tsx b/src/pages/Transaction/Transaction.tsx index 5b10b79..f3be04a 100644 --- a/src/pages/Transaction/Transaction.tsx +++ b/src/pages/Transaction/Transaction.tsx @@ -34,6 +34,23 @@ function Transaction() { const navigate = useNavigate(); + + function convertENotationToNumber(num: number | string): string { + const str = num.toString() + const match = str.match(/^(\d+)(\.(\d+))?[eE]([-\+]?\d+)$/) + if (!match) return str; + const [, integer,, tail, exponentStr ] = match + const exponent = Number(exponentStr) + const realInteger = integer + (tail || '') + if(exponent > 0) { + const realExponent = Math.abs(exponent + integer.length) + return realInteger.padEnd(realExponent, '0') + } else { + const realExponent = Math.abs(exponent - (tail?.length || 0)) + return '0.'+ realInteger.padStart(realExponent, '0') + } + } + useEffect(() => { async function fetchTransaction() { if (!hash) return; @@ -90,8 +107,14 @@ function Transaction() { newTransactionInfo.outs = parsedOuts.map(e => { const { pub_keys } = e; const pubKeys = (pub_keys instanceof Array) ? pub_keys : []; + + const existingAmount = (e?.amount / 1e12); + if (existingAmount) { + e.convertedAmount = convertENotationToNumber(existingAmount?.toExponential()); + } + return { - amount: (e?.amount / 1e12) || "-", + amount: e.convertedAmount || "-", publicKeys: pubKeys.slice(0, 4), globalIndex: e?.global_index || 0 }