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.
This question is related to
javascript
html
percentage
argument-passing
Try:
const result = Math.round((data.expense / data.income) * 100)
try
var result = (35.8 / 100) * 10;
Thanks
function calculate() {_x000D_
// amount_x000D_
var salary = parseInt($('#salary').val());_x000D_
// percent _x000D_
var incentive_rate = parseInt($('#incentive_rate').val());_x000D_
var perc = "";_x000D_
if (isNaN(salary) || isNaN(incentive_rate)) {_x000D_
perc = " ";_x000D_
} else {_x000D_
perc = (incentive_rate/100) * salary;_x000D_
_x000D_
_x000D_
} $('#total_income').val(perc);_x000D_
}
_x000D_
<div id="contentse "><br>
<h2>Percentage Calculator</h2>
<form action="/charactercount" class="align-items-center" style="border: 1px solid #eee;padding:15px;" method="post" enctype="multipart/form-data" name="form">
<input type="hidden" name="csrfmiddlewaretoken" value="NCBdw9beXfKV07Tc1epTBPqJ0gzfkmHNXKrAauE34n3jn4TGeL8Vv6miOShhqv6O">
<div style="border: 0px solid white;color:#eee;padding:5px;width:900px">
<br><div class="input-float" style="float: left;"> what is <input type="text" id="aa" required=""> % of <input type="text" id="ab"> ? </div><div class="output-float"><input type="button" class="crm-submit" value="calculate" onclick="calculatea()"> <input type="text" id="ac" readonly=""> </div><br style="clear: both;"> </div><br>
<hr><br>
<div style="border: 0px solid white;color:#eee;padding:5px;width:900px">
<div class="input-float" style="float: left;"><input type="text" id="ba"> is what percent of <input type="text" id="bb"> ? </div><div class="output-float"><input type="button" class="crm-submit" value="calculate" onclick="calculateb()"> <input type="text" id="bc" readonly=""> % </div><br style="clear: both;"></div><br>
<hr><br>
<div style="border: 0px solid white;color:#eee;padding:5px;width:900px">
Find percentage change(increase/decrease) <br><br>
<div class="input-float" style="float: left;">from <input type="text" id="ca"> to <input type="text" id="cb"> ? </div><div class="output-float"><input type="button" class="crm-submit" value="calculate" onclick="calculatec()"> <input type="text" id="cc" readonly=""> %</div><br style="clear: both;"></div>
</form>
</div>
_x000D_
Live example here: setool-percentage-calculator
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.
Heres another approach.
HTML:
<input type='text' id="pointspossible" class="clsInput" />
<input type='text' id="pointsgiven" class="clsInput" />
<button id="btnCalculate">Calculate</button>
<input type='text' id="pointsperc" disabled/>
JS Code:
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
$('#btnCalculate').on('click', function() {
var a = $('#pointspossible').val().replace(/ +/g, "");
var b = $('#pointsgiven').val().replace(/ +/g, "");
var perc = "0";
if (a.length > 0 && b.length > 0) {
if (isNumeric(a) && isNumeric(b)) {
perc = a / b * 100;
}
}
$('#pointsperc').val(perc).toFixed(3);
});
Live Sample: Percentage Calculator
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%
For percent increase and decrease, using 2 different methods:
const a = 541
const b = 394
// Percent increase
console.log(
`Increase (from ${b} to ${a}) => `,
(((a/b)-1) * 100).toFixed(2) + "%",
)
// Percent decrease
console.log(
`Decrease (from ${a} to ${b}) => `,
(((b/a)-1) * 100).toFixed(2) + "%",
)
// Alternatives, using .toLocaleString()
console.log(
`Increase (from ${b} to ${a}) => `,
((a/b)-1).toLocaleString('fullwide', {maximumFractionDigits:2, style:'percent'}),
)
console.log(
`Decrease (from ${a} to ${b}) => `,
((b/a)-1).toLocaleString('fullwide', {maximumFractionDigits:2, style:'percent'}),
)
_x000D_
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
Source: Stackoverflow.com