2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【web3.js】web3.utils.BNで大きな数字を扱う

Posted at

概要

JavaScriptで大きな数字を扱うときのライブラリBN.jsを使用出来る.
一般的にJavaScriptの数値型Numberは倍精度浮動小数点で表現されるため,最大の整数はNumber.MAX_SAFE_INTEGER=9007199254740991である.
今回のBN.jsライブラリを用いることでこの数値以上の数値を扱う計算ができる.

仕様

web3.utils.BN(mixed);
  • 引数
    Number/String : 数値,数値の文字列

  • 返り値
    Object : BN.jsのインスタンス

const BN = web3.utils.BN;

new BN(1234).toString();
> '1234'

new BN('1234').add(new BN('1')).toString();
> "1235"

new BN('0xea').toString();
> "234"

参考

web3.js ドキュメント

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?