Skip to content

A simple npm package to convert numbers to string percentages with some options.

License

Notifications You must be signed in to change notification settings

mohamedashrafothman/calc-percentage

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

calc-percentage

A simple function to convert numbers to string percentages with options.

percentage(0.12) // "12%"
percentage(1) // "100%"
percentage(-0.1) // "-10%"
percentage(1 / 3) // "33%"

percentage(0.1234) // "12%"
percentage(0.1234, 0) // "12%"
percentage(0.1234, 1) // "12.3%"
percentage(0.1234, 2) // "12.34%"

percentage(Infinity) // "∞%"

percentage(45, 50, {percSign: true}) // "90%"
percentage(100, 50, {decimal: 3,percSign: true}) // "200.000%"

Install

$ npm i calc-percentage

Usage

var { percentage } = require('calc-percentage');

percentage(0.12) // "12%"
percentage(1) // "100%"
percentage(-0.1) // "-10%"
percentage(1 / 3) // "33%"
percentage(0.1234) // "12%"

percentage(Infinity) // "∞%"

percentage(45, 50, {percSign: true}) // "90%"
percentage(100, 50, {decimal : 3,percSign: true}) // "200.000%" 
percentage(50, 100, {decimal: 1}) // "50.0"

API

calcPercent(val, total, opts = {decimal,percSign})

val

Type: number

Value which should be calculated.

total

Type: number

Total that val should be compared against.

opts.decimal

Type: number
Default: 0

Number of decimals.

opts.percSign

Type: string

Append a % sign.

License

MIT © Mohamed Ashraf Othman

About

A simple npm package to convert numbers to string percentages with some options.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published