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

stround

v0.3.1

Published

Rounding methods for exact numbers using strings.

Downloads

1,272

Readme

stround

stround provides arbitrary precision rounding of the types supported by typical number formatting libraries (e.g. NSNumberFormatter, BigDecimal) for numbers represented as strings. The purpose is to avoid floating point errors while still working with decimal values.

Install

$ npm install stround

Usage

$ node
> var stround = require('stround');
// Extract exports for examples below.
> var round = stround.round;
> var shift = stround.shift;
> var modes = stround.modes;

Round to integers if no precision is specified:

> round('5.025');
'5'
> round('12.8');
'13'

Round specifying a precision:

> round('1.18', 1);
'1.2'
> round('1.18', 2);
'1.18'
> round('1.68', 0);
'2'

The result will have the specified precision:

> round('1', 2);
'1.00'
> round('84.9', 2);
'84.90'

Round toward positive infinity:

> round('1.1', 0, modes.CEILING);
'2'
> round('-1.1', 0, modes.CEILING);
'-1'

Round toward negative infinity:

> round('1.8', 0, modes.FLOOR);
'1'
> round('-1.8', 0, modes.FLOOR);
'-2'

Round toward zero:

> round('1.8', 0, modes.DOWN);
'1'
> round('-1.8', 0, modes.DOWN);
'-1'

Round away from zero:

> round('1.1', 0, modes.UP);
'2'
> round('-1.1', 0, modes.UP);
'-2'

Round towards the nearest integer, or towards an even number if equidistant (this is the default):

> round('1.4', 0, modes.HALF_EVEN);
'1'
> round('1.5', 0, modes.HALF_EVEN);
'2'
> round('2.5', 0, modes.HALF_EVEN);
'2'
> round('-2.5', 0, modes.HALF_EVEN);
'-2'
> round('2.6', 0, modes.HALF_EVEN);
'3'

Round towards the nearest integer, or towards zero if equidistant:

> round('1.4', 0, modes.HALF_DOWN);
'1'
> round('1.5', 0, modes.HALF_DOWN);
'1'
> round('2.5', 0, modes.HALF_DOWN);
'2'
> round('-2.5', 0, modes.HALF_DOWN);
'-2'
> round('2.6', 0, modes.HALF_DOWN);
'3'

Round towards the nearest integer, or away from zero if equidistant:

> round('1.4', 0, modes.HALF_UP);
'1'
> round('1.5', 0, modes.HALF_UP);
'2'
> round('2.5', 0, modes.HALF_UP);
'3'
> round('-2.5', 0, modes.HALF_UP);
'-3'
> round('2.6', 0, modes.HALF_UP);
'3'

It is sometimes convenient to adjust the exponent of a number, too:

> shift('12', 2);
'1200'
> shift('123.4', -2);
'1.234'

And sometimes it's useful to keep the decimal parts in a tuple:

> shift([false, '12', '34'], 2)
[false, '1234', '']

Contributing

Build Status

Setup

First, install the development dependencies:

$ npm install

Then, try running the tests:

$ npm test

Pull Requests

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Any contributors to the master stround repository must sign the Individual Contributor License Agreement (CLA). It's a short form that covers our bases and makes sure you're eligible to contribute.

When you have a change you'd like to see in the master repository, send a pull request. Before we merge your request, we'll make sure you're in the list of people who have signed a CLA.