README
precise-calculator
Precise Calculator
Usage
Install globally to use repl
npm install -g precise-calculator
$ pcalc
$C> 1 + 1
2
Or install in project to use api
const $C = require('precise-calculator')
// (1 + 1) * 1 / 1 - 1
$C(1).add(1).mul(1).div(1).sub(1).v()
API
Calculator
const $C = require('precise-calculator')
$C(1)
$C('1')
new $C.Calculator(1)
new $C.Calculator('1')
/ add(n)$add(c)
Arithmetic operator +
.
$C(1).add(1)
$C(1).add('1')
$C('1').$add($C(1))
/ sub(n)$sub(c)
Arithmetic operator -
.
$C(1).sub(1)
$C(1).sub('1')
$C('1').$sub($C(1))
/ mul(n)$mul(c)
Arithmetic operator *
.
$C(1).mul(1)
$C(1).mul('1')
$C('1').$mul($C(1))
/ div(n)$div(c)
Arithmetic operator /
.
$C(1).div(1)
$C(1).div('1')
$C('1').$div($C(1))
/ round(precision)rv(precision)
Use default rounding method to round and return the value with special precision.
Default rounding method is round-half-up, which can be changed by setup()
$C(3.141).round(2) // 3.14
$C(3.145).rv(2) // 3.15
/ upRound(precision)uv(precision)
Use round-half-up to round and return the value with special precision.
$C(3.141).upRound(2) // 3.14
$C(3.145).uv(2) // 3.15
/ evenRound(precision)ev(precision)
Use round-half-even to round and return the value with special precision.
$C(3.145).evenRound(2) // 3.14
$C(3.155).ev(2) // 3.16
/ ceil(precision)cv(precision)
Ceil and return the value with special precision.
$C(3.141).ceil(2) // 3.15
$C(3.155).cv(2) // 3.16
/ floor(precision)fv(precision)
Floor and return the value with special precision.
$C(3.146).floor(2) // 3.14
$C(3.151).fv(2) // 3.15
/ r(precision)ru(precision)
/re(precision)
/rc(precision)
/rf(precision)
Use rounding method to round and return itself, usually in the middle of calculations
$C(1).div(8).r(2).mul(5).v() // 0.65
$C(1).div(8).ru(2).mul(5).v() // 0.65
$C(1).div(8).re(2).mul(5).v() // 0.6
$C(1).div(8).rc(2).mul(5).v() // 0.65
$C(1).div(8).rf(2).mul(5).v() // 0.6
r
use default rounding methodru
use round-half-up rounding methodre
use round-half-even rounding methodrc
use ceil rounding methodrf
use floor rounding method
format(fmt='#,##0.00', prefix='', suffix='')/fmt(fmt='#,##0.00', prefix='', suffix='')
Format the value with special formatter. format only, no round, if you want round please round before format.
$C(1234.1).format('#,##0.00') // 1,234.10
$C(1234.1).format('#,##0.00', '