Javascript Calculate Percentage
1
2
3
4
5
6
7
8
let num = (data[0] / (data[0] + data[1])) * 100;
if (num < 1) {
return num.toFixed(1) + "%";
} else if (num < 10) {
return num.toPrecision(2) + "%";
} else {
return Math.floor(num) + "%";
}
The result would be 0.X%, 2.3%, 99%
This post is licensed under CC BY 4.0 by the author.