Giter Club home page Giter Club logo

allocation's Introduction

Allocation

Distributes a given value based on shares. When the sum of the shares equals 100, it functions akin to percentages.

Supported Exact Calculation
Native JS floating points No
bignumber.js Yes
decimal.js Yes

Usage with Floating Point Numbers

import { distribute } from "allocation";

const result = distribute(200, [45, 55]); // [90, 110]

Usage with External Libraries

import { distribute } from "allocation";
import { BigNumber } from "bignumber.js";

const result = distribute(BigNumber(200), [45, 55]); // [BigNumber(90), BigNumber(110)]

Exact Calculations

Floating point numbers (JS native numbers) are not suitable for exact calculations.

See the below example:

const shares = [49.9, 49.9, 0.2];
const result = distribute(100, shares); // [49.9, 49.9, 0.20000000000000284]

Sometimes when the results are rounded they seem exact, but they are not, different inputs may result in inexact calculations even when rounding.

const shares = [49.9, 49.9, 0.2];
const result = distribute(100, shares, { scale: 2 }); // [49.9, 49.9, 0.2] WARNING: Seems OK, but it is not.

Use external numeric libraries where exact calculations are needed. (i.e. monetary calculations)

import { BigNumber } from "bignumber.js";

const shares = [49.9, 49.9, 0.2];
const result = distribute(BigNumber(100), shares); // [BigNumber(49.9), BigNumber(49.9), BigNumber(0.2)]

API

distribute(value, shares, { scale }): Numeric[]

This function distributes a given BigNumber.js or Decimal.js value based on shares. When the sum of the shares equals 100, it functions akin to percentages.

Parameters

Name Type Description
value Numeric is the total value to distribute.
shares (number | Numeric)[] is shares (weights) to distribute.
scale number Number of digits after decimal point to round the results.

allocation's People

Contributors

ozum 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.