Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you format balances with the Polkadot JS API?

I would like to get the balance of a user formatted using the base unit of the Substrate chain.

When I use toHuman(), I get an opinionated formatting which includes a prefix like so:

let account = await api.query.system.account("EGVQCe73TpFyAZx5uKfE1222XfkT3BSKozjgcqzLBnc5eYo");
account.data.free.toHuman()

2.8320 kKSM

While toNumber() returns the value without any units at all:

account.data.free.toNumber()

2832057009447293

How do I get this value with just the base units using the Polkadot JS API?

like image 996
Shawn Tabrizi Avatar asked Sep 17 '25 03:09

Shawn Tabrizi


1 Answers

You will need to query the chain for the chainDecimals, which will tell you how many places into the raw value you should place the decimal:

api.registry.chainDecimals

12

You can either directly parse the number by dividing by 10chainDecimals, or you can use the Polkadot JS utility function formatBalance to help you format this a bit more easily:

formatBalance(
    accountData.data.free,
    { withSi: false, forceUnit: '-' },
    chainDecimals
);

2,832.057

I ended up making a utility function like this:

function toUnit(balance, decimals) {
    base = new BN(10).pow(new BN(decimals));
    dm = new BN(balance).divmod(base);
    return parseFloat(dm.div.toString() + "." + dm.mod.toString())
}
like image 110
Shawn Tabrizi Avatar answered Sep 23 '25 11:09

Shawn Tabrizi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!