Javascript 计算百分比Javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30922008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculate percentage Javascript
提问by espresso_coffee
I have a question about javascript logic what I use to get percent of two inputs from my text fields. Here is my code:
我有一个关于 javascript 逻辑的问题,我用什么来从我的文本字段中获取两个输入的百分比。这是我的代码:
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.
出于某种原因,如果我的输入是 600 和 200,我的结果应该是 33.333,但我得到的是 3.333。如果我对我的值进行硬编码,这可以正常工作。如果有人可以帮助我很感激。提前致谢。
采纳答案by Vikash
It seems working :
它似乎工作:
HTML :
HTML :
<input type='text' id="pointspossible"/>
<input type='text' id="pointsgiven" />
<input type='text' id="pointsperc" disabled/>
JavaScript :
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);
}
});
回答by Bruno Quaresma
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%
回答by Samuel Dos Santos
To get the percentage of a number, we need to multiply the desired percentage percentby 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.
.toFixed()
对于货币格式是可选的。
回答by Sunny Vakil
Try:
尝试:
const result = Math.round((data.expense / data.income) * 100)
回答by Satinder singh
Heres another approach.
这是另一种方法。
HTML:
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:
JS代码:
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
实时示例:百分比计算器
回答by Zia Qureshi
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);
}
回答by M.A.K. Ripon
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
回答by subindas pm
try
尝试
var result = (35.8 / 100) * 10;
Thanks
谢谢