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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@toolsnap/percentage-utils

v1.0.0

Published

Lightweight utility library for percentage calculations — calculate, change, and percentage-of with zero dependencies.

Downloads

76

Readme

@toolsnap/percentage-utils

A lightweight, zero-dependency utility library for common percentage calculations in JavaScript and Node.js.

Installation

npm install @toolsnap/percentage-utils

Why This Library?

Percentage calculations are everywhere — discounts, tax rates, growth metrics, progress bars, financial analysis — yet developers frequently implement them inline with subtle bugs (division by zero, floating-point drift, sign errors). This package provides three battle-tested functions that handle edge cases and let you control precision.

For an interactive online calculator, check out the Percentage Calculator on Risetop — it covers all the same operations with a clean UI and supports batch calculations.

API

calculatePercentage(part, total, decimals = 2)

Returns what percentage part is of total.

const { calculatePercentage } = require('@toolsnap/percentage-utils');

calculatePercentage(25, 200);      // 12.5
calculatePercentage(3, 7, 4);      // 42.8571
calculatePercentage(0, 100);       // 0

Throws if total is zero or either argument is non-finite.

percentageChange(oldValue, newValue, decimals = 2)

Returns the percentage change from oldValue to newValue. Positive means increase, negative means decrease.

const { percentageChange } = require('@toolsnap/percentage-utils');

percentageChange(100, 120);        // 20
percentageChange(50, 25);          // -50
percentageChange(200, 200);        // 0
percentageChange(30, 45, 1);       // 50

Throws if oldValue is zero.

percentageOf(percentage, total, decimals = 2)

Returns what percentage% of total is.

const { percentageOf } = require('@toolsnap/percentage-utils');

percentageOf(25, 200);             // 50
percentageOf(15.5, 1000, 1);       // 155
percentageOf(0, 500);              // 0
percentageOf(100, 99);             // 99

Real-World Examples

E-commerce Discount Calculation

const { calculatePercentage } = require('@toolsnap/percentage-utils');

const originalPrice = 89.99;
const salePrice = 59.99;
const discount = calculatePercentage(originalPrice - salePrice, originalPrice);

console.log(`You save ${discount}%!`);
// You save 33.34%!

Revenue Growth Report

const { percentageChange, percentageOf } = require('@toolsnap/percentage-utils');

const q1 = 45000;
const q2 = 58000;
const growth = percentageChange(q1, q2);
const q2Target = percentageOf(15, q1); // 15% growth target

console.log(`Q2 grew ${growth}% vs target of 15%`);
console.log(`Target was $${q2Target} — actual $${q2}`);

Progress Bar

const { calculatePercentage } = require('@toolsnap/percentage-utils');

function renderProgress(completed, total) {
  const pct = calculatePercentage(completed, total, 1);
  const bar = '█'.repeat(Math.round(pct / 5)) + '░'.repeat(20 - Math.round(pct / 5));
  return `[${bar}] ${pct}%`;
}

console.log(renderProgress(73, 100));
// [█████████████░░░░░░] 73%

Browser Usage

This is a CommonJS module. For browser use, bundle with webpack, esbuild, or use the global export pattern:

<script>
// After bundling
const { calculatePercentage } = window.percentageUtils;
</script>

Related Tools

License

MIT