Calculate percentage Javascript
It seems working :
HTML :
<input type='text' id="pointspossible"/>
<input type='text' id="pointsgiven" />
<input type='text' id="pointsperc" disabled/>
JavaScript :
$(function(){
$('#pointspossible').on('input', function() {
calculate();
});
$('#pointsgiven').on('input', function() {
calculate();
});
function calculate(){
var pPos = parseInt($('#pointspossible').val());
var pEarned = parseInt($('#pointsgiven').val());
var perc="";
if(isNaN(pPos) || isNaN(pEarned)){
perc=" ";
}else{
perc = ((pEarned/pPos) * 100).toFixed(3);
}
$('#pointsperc').val(perc);
}
});
Demo : http://jsfiddle.net/vikashvverma/1khs8sj7/1/
Try:
const result = Math.round((data.expense / data.income) * 100)
You can use this
function percentage(partialValue, totalValue) {
return (100 * partialValue) / totalValue;
}
Example to calculate the percentage of a course progress base in their activities.
const totalActivities = 10;
const doneActivities = 2;
percentage(doneActivities, totalActivities) // Will return 20 that is 20%