cyou-big-number
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
Parameters
number
Returns Decimal
n2d
Type: numberToDecimalObj
decimalToString
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, default2
) -
$1.maxFixed
(optional, default10
)
-
-
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
This function is alias for decimalToString
Returns string number in string format
numberToString
This function is alias for decimalToString, just lik d2s
Returns string number in string format
n2s
This function is alias for numberToString
Returns string number in string format
decimalPlus
this function receive multiple addends and return the sum in a Decimal Object
Parameters
-
num1
(string | number | Decimal) first addends is required -
nums
(string | number | Decimal) the other addends
Returns Decimal sum
decimalPlusToString
This function is similar as decimalPlus, bug return the sum in a string
Parameters
-
num1
(string | number | Decimal) first addends is required -
nums
(string | number | Decimal) the other addends
Returns string sum
decimalMinus
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
This function is similar as decimalMinus, bug return the minuend in a string
Parameters
-
num1
(string | number | Decimal) first addends is required -
nums
(string | number | Decimal) the other subtrahend
Returns string minuend
decimalTimes
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
This function is similar as decimalTimes, bug return the product in a string
Parameters
-
num1
(string | number | Decimal) first multiplicand is required -
nums
(string | number | Decimal) the other multiplier
Returns string product
decimalDiv
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
This function is similar as decimalDiv, bug return the dividend in a string
Parameters
-
num1
(string | number | Decimal) first dividend is required -
nums
(string | number | Decimal) the other divisor
Returns string dividend
Related
- decimal.js - Consistency Made Simple
TODOS
- Enables functions to support chain calls
- add prettier budge
License
This project is licensed under the MIT license.