Skip to content

santi100a/percentage-lib

Repository files navigation

Santi's Powerful Percentage Function

Build Status npm homepage GitHub stars License Bundlephobia stats

  • 🚀 Lightweight and fast
  • 👴 ES3-compliant
  • 💻 Portable between the browser and Node.js
  • 📘 Includes TypeScript definitions

What's this?

This is a lightweight, portable NPM package to take the percentage of any number. The following is the fundamental equation of percentages:

$$ P = \frac{1}{100} \cdot n \cdot p $$

In this formula:

  • P represents the value of the percentage.
  • n represents the number over which the percentage calculation will take place.
  • p represents the percentage.

It's clearer if we look at an example:

$$ \frac{1}{100} \times 300 \times 20 = 60 $$

This means that 20% of 300 is equal to 60, which is indeed correct.

Installation

  • Via NPM: npm install @santi100a/percentage-lib
  • Via Yarn: yarn add @santi100a/percentage-lib
  • Via PNPM: pnpm install @santi100a/percentage-lib

API

  • function percentage(num: number, percentage: number): number;

    Calculates the percentage of a given number.

    Name Type Description Optional? Default
    num number The number for which the percentage needs to be calculated. No N/A
    percentage number The percentage value to calculate. Must be positive. No N/A

    Returns the calculated percentage value. Throws a TypeError If num is not a number or percentage is not a number. Throws a TypeError If percentage is not positive.

Usage

const percentage = require('@santi100a/percentage-lib'); // CJS
import percentage from '@santi100a/percentage-lib'; // ESM
import percentage = require('@santi100a/percentage-lib'); // TypeScript

const result = percentage(50, 20);
console.log(result); // Output: 10

Contribute

Wanna contribute? File an issue or pull request! Look at the contribution instructions and make sure you follow the contribution Code of Conduct.