Giter Club home page Giter Club logo

decimal-js's Introduction

Decimal

Simple decimal arithmetic for the browser and node.js!

Why?

Why don’t my numbers, like 0.1 + 0.2 add up to a nice round 0.3,
and instead I get a weird result like 0.30000000000000004?

Because internally, computers use a format (binary floating-point)
that cannot accurately represent a number like 0.1, 0.2 or 0.3 at all.

Source : http://floating-point-gui.de/

I wrote this because I needed to do simple computation in the browser and I couldn't find a lightweight library to do it.

How to use?

In the browser

<script src="lib/decimal.js"></script>

In node

npm install decimal

then in your program

var Decimal = require('decimal');

Examples

>>> 1.1 + 2.2
3.3000000000000003

>>> Decimal('1.1').add('2.2').toNumber()
3.3

>>> 4.01 * 2.01
8.060099999999998

>>> Decimal('4.01').mul('2.01').toNumber()
8.0601

>>> Decimal.mul('4.01', '2.01').toNumber()
8.0601

Can I help?

Of course you can, I suck at math, and this implementation is very naive. If you are a math Guru and you see something wrong or a way to simplify things you can send in a pull request.

Methods

Decimal(n)

Create a new Decimal from n. n can be a string, integer, or another Decimal.

.toString()

Returns the Decimal instance as a string.

.toNumber()

Turn a Decimal into a Number.

.add(n)

Return a new Decimal containing the instance value plus n.

.sub(n)

Return a new Decimal containing the instance value minus n.

.mul(n)

Return a new Decimal containing the instance value multiplied by n.

.div(n)

Return a new Decimal containing the instance value integrally divided by n.

decimal-js's People

Contributors

scottcorgan avatar

Watchers

James Cloos avatar  avatar  avatar

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.