-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
27 lines (26 loc) · 900 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
'use strict';
/**
* Percentage Function that takes two parameter and return persentage
* By: Mohamed Ashraf Othman
* @param {Number} val A part number of a whole that is needed to be in percentage format.
* @param {Number} total The whole number.
* @param {Object} opts Two oprions, first is desimal and second is percentage sign
* @return {String} Example: "20.0%".
*/
module.exports.percentage = (val, total, opts = {decimal: 0,percSign: false}) => {
if (typeof val == 'number') {
if (total) {
if (typeof total == 'number' && total === parseInt(total)) {
return `${(val / total * 100).toFixed(opts.decimal)}${(opts.percSign === true) ? "%" : ""}`;
} else {
return false;
}
} else {
return `${(Number(val) * 100).toFixed(opts.decimal)}%`;
}
} else if (typeof val === 'string' && val.toLowerCase() === "infinity") {
return "∞%";
} else {
return false;
}
};