7

Is there a simpler way to add balance transfer values without converting them to BN or Unit value first? For example, say I want to add "0x00000000000000001bc16d674ec80000", "0x000000000000021e19e0c9bab2400000" and "0x00000000000000000de0b6b3a7640000" which in units are "2.0000 Unit", "10.0000 kUnit", and "1.0000 Unit".

I have used @polkadot/util 's formatBalance with forceUnit option to get uniform units and then add. But I feel like there may be a better way.

1 Answer 1

8

I'm assuming the above are hex strings? In that case you would need to convert and add them.

import { hexToBn } from '@polkadot/util';

// convert and add
const total = hexToBn('0x00000000000000001bc16d674ec80000')
  .add(hexToBn('0x000000000000021e19e0c9bab2400000'))
  .add(hexToBn('0x00000000000000000de0b6b3a7640000'));

// format as needed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.