npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rinxun/numeric-calculator

v1.0.6

Published

Calculate addition, subtraction, multiplication, division precisely, for tackling the precision issue

Downloads

432

Readme


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.