Code Monkey home page Code Monkey logo

uint256's Introduction

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

uint256's People

Stargazers

Roamin avatar longcpp avatar William Alexander avatar Eretnek Hippi Messiás avatar

Watchers

James Cloos avatar Attila Varga avatar

uint256's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.