Giter Site home page Giter Site logo

bitcoin-math's Introduction

#bitcoin-math

Providesw number prototypes for converting between BTC and Satoshi values. It uses string based parsing, which avoids JavaScript's nasty problem of doing weird things with integer division.

Also adds a zeropad method for making full length BTC strings

If imported and used as var btcMath = require('bitcoin-math') you can use btcMath.getRandomBitcoin() and btcMath.getRandomSatoshi(). If you do not need these, you can simply import the module without assigning it to a variable and still use the number prototypes.

TOC

Number.toBitcoin()

should add the toBitcoin function to the Number object.

var num = 1;
assert(num.toBitcoin);

should return a bitcoin value derived from the number.

var satoshi = 10000000;
assert(satoshi.toBitcoin() === 0.1);

should return NaN if the original is NaN.

var bad = NaN;
assert(isNaN(bad.toBitcoin()));

Number.toSatoshi()

should add the toSatoshi function to the Number object.

var num = 1;
assert(num.toSatoshi);

should return a satoshi value derived from the number.

var bitcoin = 1;
assert(bitcoin.toSatoshi() === 100000000);

should return NaN if the original is NaN.

var bad = NaN;
assert(isNaN(bad.toSatoshi()));

Number.zeroFill()

should add the zeroFill function to the Number object.

var num = 1;
assert(num.zeroFill);

should return a decimal with zeros added (1 => 1.00000000).

var bitcoin = 1;
assert(bitcoin.zeroFill() === "1.00000000");

should return a decimal with zeros added (1.123 => 1.12300000).

var bitcoin = 1.123;
assert(bitcoin.zeroFill() === "1.12300000");

should return NaN if the original is NaN.

var bad = NaN;
assert(isNaN(bad.zeroFill()));

#getRandomSatoshi()

should reurn an integer value between the specified values with specified non zero digits.

var rand = btcMath.getRandomSatoshi(100, 10000);
assert(rand > 100);
assert(rand < 10000);
var nonZeros = rand.toString().replace(/0/g, "").length;
assert(nonZeros <= 2);

#getRandomBitcoin()

should reurn a float value between the specified values with specified non zero digits.

var rand = btcMath.getRandomBitcoin(1, 100, 4);
assert(rand > 1);
assert(rand < 100);
var nonZeros = rand.toSatoshi().toString().replace(/0/g, "").length;
assert(nonZeros <= 4);

bitcoin-math's People

Contributors

cr-nz avatar paulbdavis avatar

Watchers

 avatar

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.