Search code examples
javascriptethereumsolidity

How to convert 0x Protocol BigNumber to String in Javascript


I'm getting the market order and limit order's amount and price as a 0x Protocol BigNumber format. And I'm willing to save it as a numeric value to SQL database, so I need to convert BigNumber to String.

I used this command:

BigNumber.toString()

But I got 200000000000000000 while current BigNumber's value is 0.2.

How can I convert BigNumber to the correct numeric string?


Solution

  • Please check this function.

    https://github.com/0xProject/0x-launch-kit-frontend/blob/development/src/util/tokens.ts#L5

    const amountDecimal = tokenAmountInUnitsToBigNumber(amount, quoteTokenDecimal).toString();
    

    We can convert amount(BigNumber) value to decimal string with tokenAmountInUnitsToBigNumber.