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?
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:You can either directly parse the number by dividing by 10chainDecimals, or you can use the Polkadot JS utility function
formatBalanceto help you format this a bit more easily:I ended up making a utility function like this: