GithubHelp home page GithubHelp logo

calculatorjs's Introduction

Calculatorjs

NPM version

中文文档

> 2.1 + 2.2
4.300000000000001
> calc(' 2.1 + 2.2 ')
4.3

Usage

Using NPM:

npm install --save calculatorjs

In Node.js or Browser:

const calc = require('calculatorjs')

console.log(calc(' 0.1*(0.1+0.1) '))

Direct <script> Include

<script src="calc.js"></script>
<script>
    console.log(calc(' 0.1 * (0.1 + 0.1) '))
</script>

DOC

Arithmetic expression

calc(' 1 + (2 * 3 - 1) / 4 * -1 ')

Support + - * / ( ) minus

API

calc.add(0.1, 0.2) // 0.3
calc.sub(0.1, 0.2) // -0.1
calc.mul(0.1, 0.2) // 0.02
calc.div(0.1, 0.2) // 0.5
calc.round(0.555, 2) // 0.56

License

Distributed under MIT License.

calculatorjs's People

Contributors

fzred avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

calculatorjs's Issues

number add error

var res = calc.add("1700","1700"); //17001700

var res = calc.add("1700.00","1700.00");//3400

calc.add(0.1 + 0.2, 0)

> 0.1+0.2
0.30000000000000004
> require('calculatorjs').add(0.1, 0.2)
0.3
> require('calculatorjs').add(0.1 + 0.2, 0)
0.30000000000000004

round 精度有问题

/**

  • 四舍五入
  • @param {*} number
  • @param {*} fraction
    */
    function round(number, fraction) {
    return Math.round(number * Math.pow(10, fraction)) / Math.pow(10, fraction)
    }

round 精度有问题
round(0.285,2) = 0.28
round(0.135,2) = 0.14
目前暂时改成

function round(number, fraction) = (
return Math.round((number + Number.EPSILON) * Math.pow(10, fraction)) / Math.pow(10, fraction)
).toFixed(fraction);
}

round(0.285,2) = 0.29
round(0.135,2) = 0.14

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.