计算百分比 Javascript

Calculate percentage Javascript

我有一个关于 javascript 逻辑的问题,我使用什么从我的文本字段中获取两个输入的百分比。这是我的代码:

    var pPos = $('#pointspossible').val();
    var pEarned = $('#pointsgiven').val();

    var perc = ((pEarned/pPos) * 100).toFixed(3);
    $('#pointsperc').val(perc);

出于某种原因,如果我的输入是 600 和 200,我的结果应该是 33.333,但我得到的是 3.333。如果我硬编码我的价值观,这很好。如果有人可以提供帮助,我将不胜感激。提前致谢。

它似乎有效:

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);
    }

});

演示:http://jsfiddle.net/vikashvverma/1khs8sj7/1/

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

尝试

var result = (35.8 / 100) * 10;

谢谢

你可以用这个

function percentage(partialValue, totalValue) {
   return (100 * partialValue) / totalValue;
} 

计算课程进度基础在他们的活动中的百分比的示例。

const totalActivities = 10;
const doneActivities = 2;

percentage(doneActivities, totalActivities) // Will return 20 that is 20%

这是另一种方法。

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代码:

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);
});

实时样本: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);
    }

要获得数字的百分比,我们需要将所需的百分比 percent 乘以 number。在实践中我们将有:

function percentage(percent, total) {
    return ((percent/ 100) * total).toFixed(2)
}

用法示例:

const percentResult = percentage(10, 100);
// print 10.00

.toFixed() 对于货币格式是可选的。

尝试:

const result = Math.round((data.expense / data.income) * 100)

对于增加和减少的百分比,使用 2 种不同的方法:

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()"> &nbsp; <input type="text" id="ac" readonly="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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()"> &nbsp; <input type="text" id="bc" readonly=""> &nbsp; % </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">&nbsp;?  </div><div class="output-float"><input type="button" class="crm-submit" value="calculate" onclick="calculatec()"> &nbsp; <input type="text" id="cc" readonly="">&nbsp;%</div><br style="clear: both;"></div>
    </form>
    </div>

现场示例:setool-percentage-calculator

如何用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);
 }