javascript: calculate x% of a number

Solution 1:

var result = (35.8 / 100) * 10000;

(Thank you jball for this change of order of operations. I didn't consider it).

Solution 2:

This is what I would do:

// num is your number
// amount is your percentage
function per(num, amount){
  return num*amount/100;
}

...
<html goes here>
...

alert(per(10000, 35.8));

Solution 3:

Your percentage divided by 100 (to get the percentage between 0 and 1) times by the number

35.8/100*10000

Solution 4:

Best thing is to memorize balance equation in natural way.

Amount / Whole = Percentage / 100

usually You have one variable missing, in this case it is Amount

Amount / 10000 = 35.8 / 100

then you have high school math (proportion) to multiple outer from both sides and inner from both sides.

Amount * 100 = 358 000

Amount = 3580

It works the same in all languages and on paper. JavaScript is no exception.

Solution 5:

I use two very useful JS functions: http://blog.bassta.bg/2013/05/rangetopercent-and-percenttorange/

function rangeToPercent(number, min, max){
   return ((number - min) / (max - min));
}

and

function percentToRange(percent, min, max) {
   return((max - min) * percent + min);
}