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

more-rounding

v4.2.1

Published

A library that offers more number rounding options for JavaScript and TypeScript

Downloads

1,615

Readme

MoreRounding

A library that offers more number rounding options for JavaScript and TypeScript

Highlights

  • Supports TypeScript!
  • Supports Node and browser

Installation

Node / TypeScript

npm install more-rounding --save

Web

<script src="https://joker876.github.io/more-rounding/dist/index.min.js"></script>

Usage

More Rounding exports a single object, containing all provided functions.

import * as MoreRounding from 'more-rounding';

Basic Functions

MoreRounding.round(number: number): number

Rounds a number towards the nearest integer. Equivalent to Math.round()

  • number - the number to be rounded.
MoreRounding.round(2.3)   // 2
MoreRounding.round(-2.3)  // -2
MoreRounding.round(2.5)   // 3
MoreRounding.round(-2.5)  // -2

MoreRounding.roundUp(number: number): number

Rounds a number towards the next integer (towards Infinity). Equivalent to Math.ceil()

  • number - the number to be rounded.
MoreRounding.roundUp(2.3)   // 3
MoreRounding.roundUp(-2.3)  // -2
MoreRounding.roundUp(2.5)   // 3
MoreRounding.roundUp(-2.5)  // -2

MoreRounding.roundDown(number: number): number

Rounds a number towards the previous integer (towards negative Infinity). Equivalent to Math.floor()

  • number - the number to be rounded.
MoreRounding.roundDown(2.3)   // 2
MoreRounding.roundDown(-2.3)  // -3
MoreRounding.roundDown(2.5)   // 2
MoreRounding.roundDown(-2.5)  // -3

MoreRounding.toZero(number: number): number

Rounds a number towards the next integer (towards the number 0).

For negative numbers, it is equivalent to Math.ceil(). For positive, equivalent to Math.floor().

  • number - the number to be rounded.
MoreRounding.toZero(2.3)   // 2
MoreRounding.toZero(-2.3)  // -2
MoreRounding.toZero(2.5)   // 2
MoreRounding.toZero(-2.5)  // -2

MoreRounding.fromZero(number: number): number

Rounds a number towards the next integer (away from the number 0).

For negative numbers, it is equivalent to Math.floor(). For positive, equivalent to Math.ceil().

  • number - the number to be rounded.
MoreRounding.fromZero(2.3)   // 3
MoreRounding.fromZero(-2.3)  // -3
MoreRounding.fromZero(2.5)   // 3
MoreRounding.fromZero(-2.5)  // -3

Complex Functions

MoreRounding.toPrecision(number: number, precision?: number, mode?: string): number

Rounds a number to a certain precision.

  • number - the number to be rounded.
  • precision - the amount of decimal places to be rounded to. Positive numbers mean more decimal places, negative numbers will round to subsequent powers of 10. Optional, default 1.
  • mode - the basic rounding function to use. Optional, default "normal". Available rounding modes are:
    • normal - uses MoreRounding.round
    • up - uses MoreRounding.roundUp
    • down - uses MoreRounding.roundDown
    • to_zero - uses MoreRounding.toZero
    • from_zero - uses MoreRounding.fromZero
MoreRounding.toPrecision(2.3725)           // 2.4
MoreRounding.toPrecision(2.3725, 3)        // 2.373
MoreRounding.toPrecision(2.5131, 3, 'up')  // 2.524
MoreRounding.toPrecision(253.5, -2)        // 300

MoreRounding.toMultiple(number: number, multiple?: number, mode?: string): number

Rounds a number to the nearest multiple of a number.

  • number - the number to be rounded.
  • multiple - the multiple to round to. Optional, default 1.
  • mode - the basic rounding function to use. Optional, default "normal". Available rounding modes are:
    • normal - uses MoreRounding.round
    • up - uses MoreRounding.roundUp
    • down - uses MoreRounding.roundDown
    • to_zero - uses MoreRounding.toZero
    • from_zero - uses MoreRounding.fromZero
MoreRounding.toMultiple(2.3725)         // 2
MoreRounding.toMultiple(2.3725, 4)      // 4
MoreRounding.toMultiple(12, 5)          // 10
MoreRounding.toMultiple(13, 5)          // 15
MoreRounding.toMultiple(13, 5, 'down')  // 10
MoreRounding.toMultiple(5123, 77)       // 5159

MoreRounding.approximate(number: number, multiple?: number, mode?: string, locale?: string): string

Rounds a number to a certain long number (subsequent powers of 1,000), to a certain precision.

  • number - the number to be rounded.
  • precision - the amount of decimal places to be rounded to, on top of the approximation. Positive numbers mean more decimal places, negative numbers will round to subsequent powers of 10. Optional, default 1.
  • unit - the unit to round to. Accepts any unit from the table found below. Leaving it undefined will approximate it to the highest unit, where the approximation is in this range: 1,000 > x ≥ 1.
  • locale - if specified, the number will be formatted according to this locale. Uses Number.prototype.toLocaleString().
  • unitMap - an object used for mapping default units to custom strings. If not specified or there is no mapping for the approximation unit, the default unit string will be used.
MoreRounding.approximate(1247)                        // 1.2k
MoreRounding.approximate(351334)                      // 351.3k
MoreRounding.approximate(1354348, 2)                  // 1.35M
MoreRounding.approximate(7346834138, 0, 'M')          // 7347M
MoreRounding.approximate(7346834138, 2, 'M', 'en-US') // 7,346.83M

| Unit | Equivalent number | | ---- | ----------------- | | k | 1,000 | | M | 1,000,000 | | B | 10^9 | | T | 10^12 | | Qa | 10^15 | | Qi | 10^18 | | Sx | 10^21 | | Sp | 10^24 | | Oc | 10^27 | | No | 10^30 | | Dc | 10^33 |