@radixdlt/uint256

1.1.0 • Public • Published

uint256

Unsigned 256 bit Integer type

npm

Install

npm install uint256

Usage

import { UInt256, U256 } from 'uint256';

const now = Date.now();
const scale = 18;
const digits = U256(10).pow(scale);

const POUND = (number: number) => U256(number).mul(digits);
const toPound = (number: UInt256, fraction = false) => {
  if (!fraction) {
    return String(number.div(digits));
  }
  const str = String(number);
  if (scale < 1) {
    return str;
  }
  return `${str.substring(0, str.length - scale)}.${str.substr(-scale)}`;
};

const a = POUND(11000);
const b = POUND(20000);

console.log('scale', scale);
console.log('a', toPound(a));
console.log('b', toPound(b));
console.log('mul', toPound(a.mul(b).div(digits)));
console.log('add', toPound(a.add(b)));
console.log('div', toPound(b.div(a.div(digits)), true));
console.log('sub', toPound(b.sub(a)));
console.log('elapsed(ms)', Date.now() - now);

// scale 18
// a 11000
// b 20000
// mul 220000000
// add 31000
// div 1.818181818181818181
// sub 9000
// elapsed(ms) 8

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.1.036latest

Version History

VersionDownloads (Last 7 Days)Published
1.1.036
1.0.90

Package Sidebar

Install

npm i @radixdlt/uint256

Weekly Downloads

36

Version

1.1.0

License

Apache-2.0

Unpacked Size

35.5 kB

Total Files

10

Last publish

Collaborators

  • javier-rdx
  • rdxrussell
  • bryanrdxworks
  • radixpublishing-owner
  • alex-rdx
  • aleworm