# percent

> Percent control done right

Latest version **2.2.0** (published 2020-01-18) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install percent
pnpm add percent
yarn add percent
bun add percent
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 2.2.0 |
| Published | 2020-01-18 |
| First published | 2014-01-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8 |
| Dependencies | 0 |
| Unpacked size | 7.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 7 |
| Author | Zlatan Vasović |
| Maintainers | zdroid |
| Keywords | percentage, matching, math, calc, calculation, calculator, semver |

## Links

- npm: https://www.npmjs.com/package/percent
- Repository: https://github.com/zdroid/percent
- Homepage: https://github.com/zdroid/percent#readme
- Issues: https://github.com/zdroid/percent/issues
- npm.io page: https://npm.io/package/percent

## Alternatives

- [random-seedable](https://npm.io/package/random-seedable.md) — 27.9K weekly downloads
- [n2words](https://npm.io/package/n2words.md) — 22.2K weekly downloads
- [@stdlib/math-base-special-factorialln](https://npm.io/package/@stdlib/math-base-special-factorialln.md) — 5.7K weekly downloads
- [@stdlib/math-base-special-abs2](https://npm.io/package/@stdlib/math-base-special-abs2.md) — 1.7K weekly downloads
- [commons-math-interpolation](https://npm.io/package/commons-math-interpolation.md) — 1.4K weekly downloads

## Recent versions

- 2.2.0 (latest) — 2020-01-18
- 2.1.0 — 2019-11-17
- 2.0.0 — 2016-10-01
- 1.2.0 — 2016-09-27
- 1.1.1 — 2014-03-29
- 1.1.0 — 2014-03-19
- 1.0.3 — 2014-02-24
- 1.0.2 — 2014-02-01
- 1.0.1 — 2014-01-17
- 1.0.0 — 2014-01-07

## README

# Percent

[![Build status](https://travis-ci.org/zdroid/percent.svg?branch=master)](https://travis-ci.org/zdroid/percent)
[![devDependencies status](https://david-dm.org/zdroid/percent/dev-status.svg)](https://david-dm.org/zdroid/percent?type=dev)

> Percent control done right

Percent is a npm module which gives you nice options to control percentages.
It's like `semver` for percentages.

## Install

```bash
$ npm install percent
```

## Examples

**Calculate percentage**

```js
const percent = require('percent');

console.log(percent.calc(5, 20, 0)); // => 25
```

**Validate percentage**

```js
const percent = require('percent');

if (percent.valid(5)) { // => true
  console.log('5 is a valid percent value');
}
```

**Compare percentages**

```js
const percent = require('percent');

if (percent.lt('5%', 6)) { // => true
  console.log('a is smaller than b');
}
```

## API

```js
const percent = require('percent');
```

### `.calc()`

**Usage:** `percent.calc(value, total, decimal, [sign])`  
**Example:** `percent.calc(5, 20, 0)`

Calculates percentage from the given number (`value`) and total number
(`total`) with specified number of decimals (`decimal`).

`sign` may be boolean which turns percent sign (`%`) addition. Returns percent
value with percent sign if `sign` is `true` (it's `false` by default). If
`sign` is string, it will be used as value suffix.

### `.re`

**Usage:** `percent.re`

Returns supreme percent regexp.

### `.valid()`

**Usage:** `percent.valid(value)`  
**Example:** `percent.valid('5%')`

Checks if `value` is valid percent value. It is valid if it's a number,
number-like string (e.g. `'10'`, not `10`), or string with number and percent
sign. Spaces are allowed in strings.

### `.sign()`

**Usage:** `percent.sign(value)`  
**Example:** `percent.sign(5)`

Adds percent sign to `value`.

### `.unsign()`

**Usage:** `percent.unsign(value)`  
**Example:** `percent.unsign('5%')`

Removes percent sign(s) from `value`.

### `.clean()`

**Usage:** `percent.clean(value)`  
**Example:** `percent.clean(' 5 %  ')`

Removes percent sign(s) and spaces from `value`.

### `.convert()`

**Usage:** `percent.convert(value, [negative])`  
**Example:** `percent.convert(' 5 %  ')`

Converts percent-like string to number. Returns negative number if `negative`
is `true`.

### `.lt()`

**Usage:** `percent.lt(l, t)`  
**Example:** `percent.lt('5%', 6)`

Checks is the first argument smaller than second.

### `.gt()`

**Usage:** `percent.gt(g, t)`  
**Example:** `percent.gt('6%', 5)`

Checks is the first argument greater than second.

### `.eq()`

**Usage:** `percent.eq(e, q)`  
**Example:** `percent.eq('5%', 5)`

Checks are the arguments logically equal.

### `.neq()`

**Usage:** `percent.neq(ne, q)`  
**Example:** `percent.neq('6%', 5)`

Checks are the arguments logically unequal.

### `.satisfies()`

**Usage:** `percent.satisfies(value, min, max)`  
**Example:** `percent.satisfies(5.5, 5, 6)`

Checks does the value satisfy the given range. It will exchange `min` and `max`
values if `min` is bigger than `max`.

## License

MIT &copy; [Zlatan Vasović](https://github.com/zdroid)

---
_Source: https://npm.io/package/percent · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
