Skip to content

Commit 675762e

Browse files
[BuyWidget] Handle very large numbers in FundWallet component
1 parent 85c4ef1 commit 675762e

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

.changeset/legal-fans-enjoy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Handle very large numbers in BuyWidget

packages/thirdweb/src/react/web/ui/Bridge/FundWallet.tsx

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,7 @@ export function FundWallet({
112112
// Convert USD amount to token amount using token price
113113
const tokenAmount = usdAmount / uiOptions.destinationToken.priceUsd;
114114
// Format to reasonable decimal places (up to 6 decimals, remove trailing zeros)
115-
const formattedAmount = Number.parseFloat(
116-
tokenAmount.toFixed(6),
117-
).toString();
115+
const formattedAmount = toPlainString(Number.parseFloat(tokenAmount.toFixed(6)));
118116
setAmount(formattedAmount);
119117
};
120118

@@ -341,3 +339,36 @@ export function FundWallet({
341339
</WithHeader>
342340
);
343341
}
342+
343+
function toPlainString(num: number) {
344+
const str = num.toString();
345+
346+
// If no exponential notation, return as-is
347+
if (str.indexOf('e') === -1) {
348+
return str;
349+
}
350+
351+
// Parse exponential notation
352+
const parts = str.split('e');
353+
const coefficient = parts[0];
354+
const exponent = parseInt(parts[1] ?? '0');
355+
356+
// Handle negative exponents (small numbers)
357+
if (exponent < 0) {
358+
const zeros = '0'.repeat(Math.abs(exponent) - 1);
359+
const digits = coefficient?.replace('.', '') || '';
360+
return '0.' + zeros + digits;
361+
}
362+
363+
// Handle positive exponents (large numbers)
364+
const [integer, decimal = ''] = coefficient?.split('.') || [];
365+
const zerosNeeded = exponent - decimal.length;
366+
367+
if (zerosNeeded >= 0) {
368+
return integer + decimal + '0'.repeat(zerosNeeded);
369+
} else {
370+
const insertAt = (integer?.length ?? 0) + zerosNeeded;
371+
const result = integer + decimal;
372+
return result.slice(0, insertAt) + '.' + result.slice(insertAt);
373+
}
374+
}

0 commit comments

Comments
 (0)