admin管理员组文章数量:1134571
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.
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.
Share Improve this question edited Dec 19, 2022 at 21:38 starball 49.3k28 gold badges194 silver badges865 bronze badges asked Jun 18, 2015 at 17:32 espresso_coffeeespresso_coffee 6,11012 gold badges93 silver badges219 bronze badges 1- are you sure pPos and pEarned are those values? – michelem Commented Jun 18, 2015 at 17:38
13 Answers
Reset to default 136You 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%
Try:
const result = Math.round((data.expense / data.income) * 100)
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/
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.
Cool (unreadable) oneliner:
const percentage = ~~((pointsGiven / pointsPossible) * 100);
~~
is the same as Math.round()
Try it:
const pointsPossible = 600;
const pointsGiven = 200;
const percentage = ~~((pointsGiven / pointsPossible) * 100);
console.log(`Percentage: %${percentage}`)
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
try
function percent(quantity, percent)
{
return (quantity * percent) / 100;
}
console.log(percent(40,10))
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
function calculate() {
// amount
var salary = parseInt($('#salary').val());
// percent
var incentive_rate = parseInt($('#incentive_rate').val());
var perc = "";
if (isNaN(salary) || isNaN(incentive_rate)) {
perc = " ";
} else {
perc = (incentive_rate/100) * salary;
} $('#total_income').val(perc);
}
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'}),
)
<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>
Live example here: setool-percentage-calculator
how to calculate percentage with return
function sum (a,b){
let s = a+b
return s;
}
let total= sum(20,20)
percentage(total);
function percentage(t){
let per = t/200*100;
document.write(per);
}
try
var result = (35.8 / 100) * 10;
Thanks
本文标签: htmlCalculate percentage JavascriptStack Overflow
版权声明:本文标题:html - Calculate percentage Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736822814a1954361.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论