'Is it possible to get the balance without the decimals or a specific amount of decimals?
I am using the web3 package and I am getting the balance like so
final abi = [
'function balanceOf(address) view returns (uint)',
];
final token = web3.Contract(
'{contract address}',
abi,
web3.provider!,
);
final balance = await token.call<BigInt>(
'balanceOf',
['{walletAddress}'],
);
print('BALANCE: ${balance}');
But when I print the balance I notice it also has the decimals. How can I get the balance without the decimals? Or with a specific amount of decimals? Or read what amount of decimals have been set on the token details?
My balance is 999987 and I get 999987000000000000000000
Solution 1:[1]
You can use 'toInt()' method. Like, 'balance.toInt()'
Solution 2:[2]
Use Decimal.fromBigInt to create Decimal and then round as desired.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Salih Can |
| Solution 2 | user18309290 |
