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

math-precision

v1.0.5

Published

.round, .ceil, .floor with precision parameter: .round(1.234, 2) → 1.23

Downloads

591

Readme

math-precision

.round, .ceil, .floor with precision parameter. E.g. .round(1.234, 2)1.23

build status Dependencies

This node.js package gives you a cleaner way to round numbers using precision . It's simple, lightweight and it relies entirely on JS Math functions. It has no dependencies and it works on any node.js version. It's a pity not to use it! :)

All world wide web documentation about decimal rounding in JavaScript, including Mozilla Docs lead us to this solution:

E.g. To round 1.2347 with precision 3 you should call:
Math.round(1.2347 * 1000) / 1000 // = 1.235

which is what this package actually does behind the scenes if you call: .round(1.2347, 3) (check the source code)

The options are: to write an ugly code, to create a function and copy/paste it everywhere, to export the function from your own utility library or to require this package ... you choose! :)

Anyway there are several npm packages which does the same thing but I find this the straightforward way to do it.

It also de deals with negative precision .round(1234, -2) = 1200

See other examples or run the tests

Install

$ npm install math-precision

Examples

.round

var round = require('math-precision').round

console.log(round(1.2347, 2))        // 1.23
console.log(round(1.235, 2))         // 1.24
console.log(round(1.2, 2))           // 1.2
console.log(round(1.24569, 3))       // 1.246
console.log(round(1234, -2))         // 1200
console.log(round(12785.9, -3))      // 13000
console.log(round(1.2347))           // 1
console.log(round(1.2347, NaN))      // 1
console.log(round(undefined, 2))     // NaN
console.log(round(NaN, 2))           // NaN

.ceil

var ceil = require('math-precision').ceil

console.log(ceil(1.2347, 2))         // 1.24
console.log(ceil(1.2, 2))            // 1.2
console.log(ceil(1.2341, 3))         // 1.235
console.log(ceil(1234, -2))          // 1300
console.log(ceil(123436.87, -3))     // 124000
console.log(ceil(1.2347))            // 2
console.log(ceil(1.2347, NaN))       // 2
console.log(ceil(undefined, 2))      // NaN
console.log(ceil(NaN, 2))            // NaN

.floor

var floor = require('math-precision').floor

console.log(floor(1.2361, 2))        // 1.23
console.log(floor(1.2367, 3))        // 1.236
console.log(floor(1.8, 2))           // 1.8
console.log(floor(1876, -2))         // 1800
console.log(floor(187697.78, -3))    // 187000
console.log(floor(1.2361))           // 1
console.log(floor(1.2361, NaN))      // 1
console.log(floor(undefined, 2))     // NaN
console.log(floor(NaN, 2))           // NaN

using math object

var math = require('math-precision')

console.log(math.round(1.2358, 2))   // 1.24
console.log(math.ceil(1.2358, 2))    // 1.24
console.log(math.floor(1.2358, 2))   // 1.23

Test

Simply clone the repo, npm install, and run npm test

License

MIT