介紹如何在 javascript 處理64位大數。
方案1
long.js 庫
介紹
A Long class for representing a 64 bit two’s-complement integer value derived from the Closure Library for stand-alone use and extended with unsigned support.
簡單使用
var Long = require("long");
var longVal = new Long(0xFFFFFFFF, 0x7FFFFFFF);
console.log(longVal.toString());
優勢
- 可以把服務器發過來的高位、底位數據合成一個大數。顯示,+、-、*、/、。
方案2
介紹
BigNumber.js is a light javascript library for node.js and the browser. It supports arithmetic operations on Big Integers.
It is build with performance in mind, uses the fastest algorithms and supports all basic arithmetic operations (+, -, *, /, %, ^, abs). Works with both positive and negative big integers.
簡單介紹
var BigNumber = require('big-number');
BigNumber(5).plus(97).minus(53).plus(434).multiply(5435423).add(321453).multiply(21).div(2).pow(2);
// 760056543044267246001
優勢
- 支持add/plus, minus/subtract, multiply/mult, divide/div, power/pow, mod, equals, lt, lte, gt, gte, isZero, abs
參考
- https://imweb.io/topic/581a8eb02373d1aa606c8be7