1.0.6 • Published 1 year ago

@rinxun/numeric-calculator v1.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

It is available as an npm package.

// with npm
npm install @rinxun/numeric-calculator

// with yarn
yarn add @rinxun/numeric-calculator

This project is licensed under the terms of the MIT license.

0.1 + 0.2 = 0.30000000000000004; 
0.1 + 0.2 - 0.3 = 5.551115123125783e-17;
  • new NC(operand: IOperand, config: IConfig)

    • number: Initial numeric

      • optional
      • type IOperand = instance | number | string
    • config: Configurations

      • optional

      • config = {
          precision?: number, // global default precision, default to 15, should be in the range 0 - 20, effects `toPrecision` when `toPrecision` has no args
          fractionDigits?: number, // global default decimal length, default to 2, should be in the range 0 - 20, effects `toFixed` when `toFixed` has no args
          enableCheckBoundary?: boolean // if true, it will check if the value is out of the safe boundary
        }

The instance supports chain operations.

  • plus(operands: IOperand[]) => instance
  • minus(operands: IOperand[]) => instance
  • times(operands: IOperand[]) => instance
  • divide(operands: IOperand[]) => instance
  • toPrecision(precision?: number) => number
  • toFixed(fractionDigits?: number) => string
// CommonJS
const { default: NC } = require('@rinxun/numeric-calculator');

// ES6 
import NC from '@rinxun/numeric-calculator';
const inst1 = new NC(); 
// or with initial numeric
const inst2 = new NC(10); 
const inst3 = new NC('10'); 
const inst4 = new NC(new NC(10)); 
// or with config
const inst5 = new NC(10, { enableCheckBoundary: true }); 
const result = new NC().plus(2.134).toPrecision(); // 2.134
const result = new NC().plus(2.1341, new NC(1.09).plus(2), '0.65').toPrecision(); // => 2.1341+(1.09+2)+0.65 = 5.8741
const result = new NC(10).plus(2.134).toPrecision(); // => 10+2.134 = 12.134
const result = new NC(10).plus(2.1341, '3.09', 0.65).toPrecision(); // => 10+2.1341+3.09+0.65 = 15.8741
const result = new NC().minus(0.4).toPrecision(); // 0.4
const result = new NC().minus(0.25, new NC(2.5).minus(1), '3.498').toPrecision(); // => 0.25-(2.5-1)-3.498 -4.748
const result = new NC(2).minus(0.4).toPrecision(); // => 2-0.4 = 1.6
const result = new NC(-2).minus(0.25, 1.5, 3.498).toPrecision(); // -2-0.25-1.5-3.498 = -7.248
const result = new NC().times(0.12).toPrecision(); // 0.12
const result = new NC().times(0.12, new NC(8).times(12.5), '5').toPrecision(); // => 0.12*(8*12.5)*5 = 60
const result = new NC(-4).times(0.12).toPrecision(); // => -4*0.12 = -0.48
const result = new NC(3.2).times(0.12, '100', 5).toPrecision(); // => 3.2*0.12*100*5 = 192
const result = new NC().divide(3).toPrecision(); // 3
const result = new NC().divide(100, new NC(60).divide(3), '2').toPrecision(); // => 100/(60/3)/2 = 2.5
const result = new NC(36.12).divide(3).toPrecision(); // => 36.12/3 = 12.04
const result = new NC(-1000).divide(100, '20', 2).toPrecision(); // -1000/100/20/2 = -0.25
const result = new NC(1.2345).toPrecision(); // 1.2345
const result = new NC(1.2345).toPrecision(4); // 1.234
const result = new NC(1.2e-2).toPrecision(); // 0.012

Follow the Banker's Rounding rules

const result = new NC(1.2345).toFixed(); // '1.23'
const result = new NC(1.2345).toFixed(3); // '1.234'
const result = new NC(1.2346).toFixed(3); // '1.235'
const result = new NC(1.2e-2).toFixed(); // '0.01'
const result = new NC(10).plus(2).minus(4).times(2).divide(8).toPrecision(); // => (10+2-4)*2/8 = 2
const result = new NC(10).plus(new NC().times(2, 4), '6').divide(new NC(12).minus(4)).toPrecision(); // => (10+(2*4)+6)/(12-4) = 3

If you have recently updated, please read the changelog for details of what has changed.