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

symmetric-round

v1.0.5

Published

A tiny utility function to perform symmetric rounding (a.k.a. "commercial rounding") on a number.

Downloads

14

Readme

symmetric-round

Build Status Coverage Status

A tiny utility function (like - literally; the function is a one-liner) to perform symmetric rounding to integer Numbers.

Install

npm install --save symmetric-round

Use

If your project only "speaks" CommonJS, you can use:

const symmetricRound = require('symmetric-round');

...but if you're transpiling with (or otherwise have) full ES6 support:

import symmetricRound from 'symmetric-round';'

...should work as well. At the time of writing (July 2019), native full ES6 import/export support on JavaScript-engines was still making its entrance to the JavaScript ergosphere.

Why this package exists?

Math.round() - is defined in the latest definition of ECMA-262 (as well as the earlier versions thereof - at the time of writing) - i.e. "The JavaScript Standard" - in such way that

  • when the input value is exactly half way between two integer Numbers, the result is the Number value that is closer to positive infinity.

This means that the standard states that the absolute value of rounding negative and positive values is asymmetric by definition; e.g.:

  • Math.round(99.5) results in 100 while
  • Math.round(-99.5) results in -99 ... and ...

...this is just one of the weirdnesses of JavaScript that may sometimes come to bite you in the hindside when you least expect it.

In practice - this leads to all sorts of hard-to-pinpoint bugs in your code like:

  • calculating discount reimbursements might get a different value than what the original discount was
  • drawing charts may be "a pixel off" when they reside on the the negative side of chart origo, etc.

...and those are just examples of the situations I have personally witnessed. All because Math.round() does not work the way I was taught at school back in the day when dinosaurs roamed the Earth.

So... Let's round numbers symmetrically instead!

This "tie-breaking method" of rounding Numbers to Integers is known by many names, such as "round half away from zero", "round half towards nearest infinity", "commercial rounding" or "symmetric rounding".

With "symmetric rounding", the positive and negative numbers are treated symmetrically and therefore the method is free of overall positive/negative bias (assuming the original numbers are positive or negative with equal probability).

Furthermore, this method of rounding is often used for currency conversions and price roundings etc.

import symmetricRound from 'symmetricRound';

symmetricRound(99.5);  // 100
symmetricRound(-99.5); // -100

Caveats?

For input values which round to "exactly zero", it must be kept in mind that even while using this function, the output value does retain the sign of the input value. While this might seem as a blemish, it is a feature caused by the way JavaScript handles Numbers and ... since it actually does retain the symmetricity of rounding I saw no reason to change that behaviour.

In real life, this is insignificant since:

(-0) === (+0); // true
(-0) === (0);  // true
(+0) === (0);  // true

However, it is good to keep in mind that:

Object.is((-0), (+0)); // false
Object.is((-0), (0));  // false
Object.is((+0), (0));  // true