How decimals work
Tokens store amounts as whole numbers called base units. Thedecimals value says where the decimal point goes when you show the amount to a person.
The USDT addresses are in Contract addresses:
0xaBabc7Ddc03e501d190C676BF3d92ef0e6e87a3C on mainnet and 0x75edC9335175Fc0552D51D48439F229c10420fe3 on testnet.
Prerequisites
- Node.js 22 or later.
Steps
1
Create the project
2
Convert amounts
usdt-decimals.ts
parseUnits(text, decimals)turns what a user typed into base units. Use it before every contract call.formatUnits(value, decimals)turns base units into text. Use it before you show a value.- Both work with
bigint, so you don’t lose precision.
3
Run it
Verify it worked
Output
Common mistakes
- Hardcoding 18.
parseEtherandformatEtherassume 18 decimals. Never use them for USDT. - Using JavaScript numbers.
Number(balance) / 1e6loses precision for large values. Keep amounts asbigintand format at the end. - Mixing tokens in math. A V2 quote for WBOT to USDT returns USDT base units (6 decimals) for an input in WBOT base units (18 decimals). Format each side with its own decimals.
- Assuming USDT has the same decimals everywhere. USDT on BNB Smart Chain has 18 decimals. Ethereum and Tron use 6. Convert when you bridge. See Bridge overview.
- Trusting the symbol. Anyone can deploy a token called USDT. Check the address.
Troubleshooting
A balance shows as 0.000000000000021366
A balance shows as 0.000000000000021366
You formatted a 6-decimal value with 18 decimals. Read
decimals from the token and pass it to formatUnits.A transfer fails with an insufficient balance error
A transfer fails with an insufficient balance error
Check the amount you passed. If it came from
parseEther, it’s 10^12 times too large for USDT.Next steps
Wrap BOT
Turn native BOT into the WBOT token.
Swap on BDEX V2
Swap WBOT for USDT.