So the question is pretty simple. I am calling contract method that returns uint256 value. Is it possible to get that value in JavaScript at all? As far as I understand JavaScript can't work with big numbers and the solution would be to make new contract that returns data as string? Right now I am getting a value that simply cuts a lot of zeros :)
Web3 returns primitive data types (including uint
) as strings. If you try to parse it into javascript native number with something like parseInt
, it will fail.
To work with the big numbers received via web3, you have to use library which can handle them. One such library is BigNumber.JS
https://github.com/MikeMcl/bignumber.js/
So you can then use it like this:
let result = BigNumber(await bigNumber.methods.getNum1().call());
and use math operations implemented by BigNumber.js
result = result.plus("99999999999999999999999999999999999999");
note that if you then simply try to use the result value itself
console.log(result);
you will get result like this
BigNumber {
s: 1,
e: 38,
c: [ 10000000213, 12312312312312, 45465777775431 ] }
Therefore you will probably want to cast it to the string in the end
console.log(result.toString());
to get result in readable form, like:
1.00000002131231231231231245465777775431e+38
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.