GithubHelp home page GithubHelp logo

round-half-even's Introduction

Round Half to Even

A utility for tie-breaking rounding, also called Banker's Rounding or Commercial Rounding.

Read more: https://en.wikipedia.org/wiki/Rounding#Round_half_to_even

Demo

https://rawgit.com/schowdhuri/round-half-even/master/demo/index.html

Installation

Using npm:

npm install --save round-half-even

Or yarn:

yarn add round-half-even

Usage

roundHalfEven(floatingPointValue, numberOfDecimals);

Node.js

import roundHalfEven from "round-half-even";
roundHalfEven(1.435, 2); // returns 1.44

Within the browser:

<script src="https://unpkg.com/round-half-even"></script>
<script>
  roundHalfEven(1.435, 2); // returns 1.44
</script>

Development

Make changes to src/index.js. Generate the browser-ready package:

npm run build

or

yarn build

Run tests:

npm test

or

yarn test

round-half-even's People

Contributors

dependabot[bot] avatar rogersnick avatar schowdhuri avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

round-half-even's Issues

Unable to handle exponential notational numbers

in index.js:

const strNum = `${value}`.replace(/0+$/, "");

If an exponential number is passed e.g. 1e-8, the strNum will be "1e-8", which does not have a decimal and subsequently is unable to be rounded.

roundHalfEven(1e-8, 2);        // expected: 0.00, actual: 1e-8
roundHalfEven(0.000000010, 2); // expected: 0.00, actual: 1e-8

Might want to consider doing a value.toFixed(20) in there, to coerce it into decimal notation. On second thought this will pre-round the value and most likely not work as intended.

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.