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
Share:
240,255
espresso_coffee
Author by

espresso_coffee

Updated on October 26, 2021

Comments

  • espresso_coffee
    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
      michelem almost 9 years
      are you sure pPos and pEarned are those values?
  • espresso_coffee
    espresso_coffee almost 9 years
    What if values are empty?
  • Vikash
    Vikash almost 9 years
    for that you need to check first. I have just given you a POC that it works.
  • espresso_coffee
    espresso_coffee almost 9 years
    I had that already. I do not need hard coded values. Thanks anyway.
  • Vikash
    Vikash almost 9 years
    Then how you are getting value 3.333?
  • espresso_coffee
    espresso_coffee almost 9 years
    I do not know, that's the issue. I'm passing entered values from input field and I'm getting that output.
  • espresso_coffee
    espresso_coffee almost 9 years
    Do you know how to reset this function every time after I click on my input text field? Thanks in advance.
  • Vikash
    Vikash almost 9 years
  • subindas pm
    subindas pm about 6 years
    @BrunoQuaresma but its worked well for me for one calculation purpose.
  • executable
    executable about 5 years
    Please give some explanation
  • Habib
    Habib almost 5 years
    This is static formula and we cannot relay on static things in development.
  • Nathan Smiechowski
    Nathan Smiechowski over 4 years
    A bit condescending for an answer.
  • jefelewis
    jefelewis over 3 years
    toFixed() will type case the value into a string.
  • Siddhartha Chowdhury
    Siddhartha Chowdhury about 3 years
    Just a note: this will return NaN if partialValue = 0 and totalValue = 0
  • mowx
    mowx almost 3 years
    Thanks so much. Sir. You helped me.
  • Maqsood Ahmed
    Maqsood Ahmed almost 3 years
    you should share general formula not static values.
  • Admin
    Admin over 2 years
    As 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
    Sebastian Schürmann over 2 years
    @jefelewis: You can wrap the result into a Number() call and get a number back, removing trailing zeroes.