Calculate percentage Javascript
240,255
Solution 1
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/
Solution 2
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%
Solution 3
Try:
const result = Math.round((data.expense / data.income) * 100)
Solution 4
To get the percentage of a number, we need to multiply the desired percentage percent by that number. In practice we will have:
function percentage(percent, total) {
return ((percent/ 100) * total).toFixed(2)
}
Example of usage:
const percentResult = percentage(10, 100);
// print 10.00
.toFixed()
is optional for monetary formats.
Solution 5
var number = 5000;
var percentX = 12;
var result;
function percentCalculation(a, b){
var c = (parseFloat(a)*parseFloat(b))/100;
return parseFloat(c);
}
result = percentCalculation(number, percentX); //calculate percentX% of number
Author by
espresso_coffee
Updated on October 26, 2021Comments
-
espresso_coffee over 2 years
I have a question about javascript logic what I use to get percent of two inputs from my text fields. Here is my code:
var pPos = $('#pointspossible').val(); var pEarned = $('#pointsgiven').val(); var perc = ((pEarned/pPos) * 100).toFixed(3); $('#pointsperc').val(perc);
For some reason if my inputs are 600 and 200, my result suppose to be 33.333 but I'm getting 3.333. If I hard code my values this works fine. If anyone can help I appreciate that. Thanks in advance.
-
michelem almost 9 yearsare you sure pPos and pEarned are those values?
-
-
espresso_coffee almost 9 yearsWhat if values are empty?
-
Vikash almost 9 yearsfor that you need to check first. I have just given you a POC that it works.
-
espresso_coffee almost 9 yearsI had that already. I do not need hard coded values. Thanks anyway.
-
Vikash almost 9 yearsThen how you are getting value 3.333?
-
espresso_coffee almost 9 yearsI do not know, that's the issue. I'm passing entered values from input field and I'm getting that output.
-
espresso_coffee almost 9 yearsDo you know how to reset this function every time after I click on my input text field? Thanks in advance.
-
Vikash almost 9 yearsLet us continue this discussion in chat.
-
subindas pm about 6 years@BrunoQuaresma but its worked well for me for one calculation purpose.
-
executable about 5 yearsPlease give some explanation
-
Habib almost 5 yearsThis is static formula and we cannot relay on static things in development.
-
Nathan Smiechowski over 4 yearsA bit condescending for an answer.
-
jefelewis over 3 yearstoFixed() will type case the value into a string.
-
Siddhartha Chowdhury about 3 yearsJust a note: this will return
NaN
ifpartialValue = 0
andtotalValue = 0
-
mowx almost 3 yearsThanks so much. Sir. You helped me.
-
Maqsood Ahmed almost 3 yearsyou should share general formula not static values.
-
Admin over 2 yearsAs it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
-
Sebastian Schürmann over 2 years@jefelewis: You can wrap the result into a Number() call and get a number back, removing trailing zeroes.