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

complete-round

v1.4.3

Published

A function to offer more rounding options for numbers in JavaScript and TypeScript

Downloads

8

Readme

complete-round

npm Travis (.com) codecov npm GitHub top language GitHub code size in bytes NPM

A function to offer more rounding options for numbers in JavaScript and TypeScript

Function purposes:

  • Offer the same functionality as Math.round()
  • Allow the user to specify a different rounding accuracy (round to the nearest 10 or 100 for example)
    • Default: 1
  • Allow the user to specify the direction in which to round
    • Closest (default) (the only option offered by Math.round())
    • Up
    • Down
    • Away from 0
    • Towards 0
  • allow the user to specify an offset
    • Default: 0
  • NEW allow supression of error reporting (not recommended in production!)
    • Default: false

Installation

npm install complete-round

<script src="https://unpkg.com/complete-round"></script>

Usage

completeRound(number[, rounding[, direction[, offset[, supressErrors]]]]);

  • rounding defaults to 1

    Must always be a number

  • direction defaults to 'closest'

    Must be one of closest, up, down, away (from 0) or towards (0)

    • up will always move up the number line, for positive numbers this will be away from 0 and for negative numbers it will be towards 0.
    • down will always move down the number line, for positive numbers this will be towards 0 and for negative numbers it will be away from 0.
    • away will always move away from 0, for positive numbers this has the effect of rounding up and for negative numbers it has the effect of rounding down.
    • towards will always move towards 0, for positive numbers this has the effect of rounding down and for negative numbers it has the effect of rounding up.
  • offset defaults to 0

    Must always be a number

    • This is useful, for example, if you want to round a number to the nearest number that ends in 7 (7, 17, 27 etc.), you would set the rounding to 10 and set the offset to 7. Be careful however, as this example will result in any negative numbers being rounded to the nearest number ending in 3.
  • supressErrors defaults to false

    Must be a boolean

    • CARE NEEDED - this will, by definition, not report any errors, and, if such an error occurs, it will only return the number it was given to round.

Passing tests

completeRound(3.5, 1, 'down')

✔️ should return 3

completeRound(3.5, 1, 'up')

✔️ should return 4

completeRound(3.5, 1, 'away')

✔️ should return 4

completeRound(3.5, 1, 'towards')

✔️ should return 3

completeRound(3.5, 1, 'closest')

✔️ should return 4

completeRound(-3.5, 1, 'down')

✔️ should return -4

completeRound(-3.5, 1, 'up')

✔️ should return -3

completeRound(-3.5, 1, 'away')

✔️ should return -4

completeRound(-3.5, 1, 'towards')

✔️ should return -3

completeRound(-3.5, 1, 'closest')

✔️ should return -4

completeRound(0.1, 0.5, 'up')

✔️ should return 0.5

completeRound(0.1, 0.5, 'down')

✔️ should return 0

completeRound(0.1, 0.5, 'away')

✔️ should return 0.5

completeRound(0.1, 0.5, 'towards')

✔️ should return 0

completeRound(0, 107, 'away')

✔️ should return 0

completeRound(1, 107, 'away')

✔️ should return 107

completeRound(-1, 107, 'away')

✔️ should return -107

completeRound(2435, 240, 'up')

✔️ should return 2640

completeRound(-5.5, 10, 'down')

✔️ should return -10

completeRound(-9785, 100000, 'away')

✔️ should return -100000

completeRound(-9785, 100000, 'towards')

✔️ should return 0

completeRound(-99999.999, 100000, 'towards')

✔️ should return 0

completeRound(0.34, 0.1, 'towards')

✔️ should return 0.3

completeRound(0.34, 0.1, 'down')

✔️ should return 0.3

completeRound(0.34, 0.1, 'closest')

✔️ should return 0.3

completeRound(0.1, 0.4325, 'up')

✔️ should return 0.4325

completeRound(0.1, 0.4325, 'down')

✔️ should return 0

completeRound(0.1, 0.4325, 'away')

✔️ should return 0.4325

completeRound(0.1, 0.4325, 'towards')

✔️ should return 0

completeRound(0.1, 0.4325, 'closest')

✔️ should return 0

completeRound(0.21625, 0.4325, 'closest')

✔️ should return 0.4325

completeRound(0.2162499999, 0.4325, 'closest')

✔️ should return 0

completeRound(0.21625000001, 0.4325, 'closest')

✔️ should return 0.4325

completeRound(5, 10, 'closest', 7)

✔️ should return 7

completeRound(5, 10, 'up', 7)

✔️ should return 7

completeRound(5, 10, 'down', 7)

✔️ should return -3

completeRound(5, 10, 'away', 7)

✔️ should return 7

completeRound(5, 10, 'towards', 7)

✔️ should return -3

completeRound(-7, -9, 'closest', -3)

✔️ should return -3

completeRound(-7, -9, 'up', -3)

✔️ should return -3

completeRound(-7, -9, 'down', -3)

✔️ should return -12

completeRound(-7, -9, 'away', -3)

✔️ should return -12

completeRound(-7, -9, 'towards', -3)

✔️ should return -3

completeRound(-0.45, 0.7, 'closest', 0.034)

✔️ should return -0.666

completeRound(-0.45, 0.7, 'up', 0.034)

✔️ should return 0.034

completeRound(-0.45, 0.7, 'down', 0.034)

✔️ should return -0.666

completeRound(-0.45, 0.7, 'away', 0.034)

✔️ should return -0.666

completeRound(-0.45, 0.7, 'towards', 0.034)

✔️ should return 0.034

completeRound(-0.45, 0, 'towards', 0.034, true)

✔️ should return -0.45

completeRound(-0.45, 0.7, 'arriba', 0.034, true)

✔️ should return -0.45