Have ideas to improve npm?Join in the discussion! »

    floating

    3.0.0 • Public • Published

    floating

    Build Status Coverage Status Code Climate

    Simple wrapper for basic arithmetic—addition, subtraction, multiplication and division.

    Useful for plugging it into arbitrary-precision or linear-arbitrary-precision as a fallback to specialised arbitrary precision libraries.

    Install

    npm i floating
    

    Usage

    Factory and configuration

    var Floating = require('floating')();
     
    new Floating(1).div(new Floating(3).valueOf(); // => 1/3

    Operations

    new Floating(0.1).plus(new Floating(0.2)).valueOf(); // => 0.1 + 0.2
     
    new Floating(0.3).minus(new Floating(0.1)).valueOf(); // => 0.3 - 0.1
     
    new Floating(0.6).times(new Floating(3)).valueOf(); // => 0.6 * 3
     
    new Floating(0.3).div(new Floating(0.2)).valueOf(); // => 0.3 / 0.2

    toString, valueOf and toJSON

    var floatingThird = new Floating(1).div(new Floating(3));
     
    floatingThird.toString(); // => '0.3333333333333333'
     
    floatingThird.valueOf() === floatingThird.toJSON(); // => true
     
    Number(floatingThird); // => 1/3

    JSON.stringify and JSON.parse with reviver

    var stringified = JSON.stringify(floatingThird); // => '0.3333333333333333'
     
    JSON.parse(stringified, Floating.reviver); // => new Floating(0.3333333333333333)

    See spec.

    Related projects

    Install

    npm i floating

    DownloadsWeekly Downloads

    90

    Version

    3.0.0

    License

    MIT

    Last publish

    Collaborators

    • avatar