Code Monkey home page Code Monkey logo

currency.js's Introduction

currency.js Build Status npm bower

currency.js is a lightweight javascript library for working with currency values. It was built to work around floating point issues in javascript. This talk by Bartek Szopka explains in detail why javascript has floating point issues.

currency.js works with values as integers behind the scenes, resolving some of the most basic precision problems.

2.51 + .01;                   // 2.5199999999999996
currency(2.51).add(.01);      // 2.52

2.52 - .01;                   // 2.5100000000000002
currency(2.52).subtract(.01); // 2.51

This should work for most reasonable values of currencies. As long as your currency values are less than 253 (in cents) or 90,071,992,547,409.91 you should be okay.

Examples

currency(123.50).add(0.23);       // 123.73
currency(5.00).subtract(0.50);    // 4.50
currency(45.25).multiply(3);      // 135.75
currency(1.12).distribute(5);     // [0.23, 0.23, 0.22, 0.22, 0.22]

Also included is a basic formatter, this allows you to work with and output strings.

currency('2,573,693.75').add('100,275.50').format();  // '2,673,969.25'
currency('1,237.72').subtract(300).format();          // '937.72'

You can also change the format, converting to international values.

currency.settings.separator = '.';
currency.settings.decimal = ',';
currency('2.573.693,75').add('100.275,50').format();  // '2.673.969,25'
currency('1.237,72').subtract(300).format();          // '937,72'

View more examples and documentation at scurker.github.io/currency.js.

Install via npm

  • The latest version npm install currency.js
  • Or GitHub master branch npm install scurker/currency.js

Install via bower

  • The latest version bower install currency.js
  • Or GitHub master branch bower install scurker/currency.js

Local Development

Running locally? Everything is built with Grunt.

  1. npm install
  2. grunt

currency.js's People

Contributors

scurker avatar bgentry avatar

Watchers

David Sanders 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.