cyou-big-number

1.0.6 • Public • Published

cyou-big-number

Travis (.com) Codecov NPM npm npm npm bundle size GitHub last commit

Simple decimal arithmetic for the browser and node.js

This package is based on decimal.js-light and offer some very simple API for big decimal.

  • {number | string} transform to decimal
  • {decimal} to fixed format
  • decimal arithmetic add/subtract/multiply/divide

Now, see you big number.

Installation

$ npm install cyou-big-number --save

Usage

In the Browser

<script src="dist/umd/index.umd.js"></script>

In node

import {numberToString, decimalToString, decimalPlus, decimalMinus, decimalTimes, decimalDiv} from 'cyou-big-number'

Examples

// Supports at least 20 integers to 10 decimals
numberToString('123456789012345678790.12345678901234567890'); // '123456789012345678790.123456789'
numberToString('9e-10'); // '0.0000000009'
numberToString(9e-10); // '0.0000000009'

// helpful format
numberToString('1.12345000000000'); // '1.12345'
numberToString('1.12345000000000', {minFixed: 10}); // '1.1234500000'
numberToString('1.12345000000000', {maxFixed: 2}); // '1.12'

// No loss of accuracy
decimalToString(decimalPlus(0.1, 0.2)); // '0.30'

decimalToString(decimalMinus(5, 1, 1, 1, 1, 1)); // '0.00'

numberToString(
    decimalTimes(Number.MAX_SAFE_INTEGER, 1000)
); // '9007199254740991000'

numberToString(
  decimalDiv(1000, 1000, 1000, 1000)
); // '0.000001'

API

numberToDecimalObj

src/index.js:9-17

Parameters

  • number

Returns Decimal

n2d

src/index.js:24-24

Type: numberToDecimalObj

decimalToString

src/index.js:38-45

transform String/Number/Decimal to a fixed format 1. minFixed <= decimal part length <= maxFixed 2. decimal part will no keep 0 at the tail, unless decimal part length is small than minFixed

Parameters

  • number (string | number | Decimal)
  • $1 Object (optional, default {})
    • $1.minFixed (optional, default 2)
    • $1.maxFixed (optional, default 10)
  • minFixed default to be 2, it means 1.0 will be transformed to 1.00, but 1.000 will not be effected this param will keep 0 at the tail of number
  • maxFixed default to be 10, it means Math.fixed(10) and this param will no keep 0 at the tail of number, it means 1.11000 will transform to 1.11, bug 1.11111 will not be effected

Returns (string | string) number in format

d2s

src/index.js:52-52

This function is alias for decimalToString

Returns string number in string format

numberToString

src/index.js:59-59

This function is alias for decimalToString, just lik d2s

Returns string number in string format

n2s

src/index.js:66-66

This function is alias for numberToString

Returns string number in string format

decimalPlus

src/index.js:75-77

this function receive multiple addends and return the sum in a Decimal Object

Parameters

Returns Decimal sum

decimalPlusToString

src/index.js:86-88

This function is similar as decimalPlus, bug return the sum in a string

Parameters

Returns string sum

decimalMinus

src/index.js:97-99

this function receive one minuend and multiple subtrahend and return the subtract output in a Decimal Object

Parameters

  • num1 (string | number | Decimal) first number is minuend, is required
  • nums (string | number | Decimal) the other subtrahend

Returns Decimal minuend

decimalMinusToString

src/index.js:108-110

This function is similar as decimalMinus, bug return the minuend in a string

Parameters

Returns string minuend

decimalTimes

src/index.js:119-121

this function receive one multiplicand and multiple multiplier, return the product in a Decimal Object

Parameters

  • num1 (string | number | Decimal) first number is multiplicand, is required
  • nums (string | number | Decimal) the other multiplier

Returns Decimal the product in a Decimal Object

decimalTimesToString

src/index.js:130-132

This function is similar as decimalTimes, bug return the product in a string

Parameters

Returns string product

decimalDiv

src/index.js:141-143

this function receive one dividend and multiple divisor, return the product in a Decimal Object

Parameters

  • num1 (string | number | Decimal) first number is dividend, is required
  • nums (string | number | Decimal) the other divisor

Returns Decimal the dividend in a Decimal Object

decimalDivToString

src/index.js:152-154

This function is similar as decimalDiv, bug return the dividend in a string

Parameters

Returns string dividend

Related

TODOS

  • Enables functions to support chain calls
  • add prettier budge

License

This project is licensed under the MIT license.

Readme

Keywords

none

Package Sidebar

Install

npm i cyou-big-number

Weekly Downloads

1

Version

1.0.6

License

MIT

Unpacked Size

93.5 kB

Total Files

15

Last publish

Collaborators

  • teeeemoji