Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript: calculate x% of a number

I am wondering how in javascript if i was given a number (say 10000) and then was given a percentage (say 35.8%)

how would I work out how much that is (eg 3580)

like image 760
Hailwood Avatar asked Dec 07 '10 02:12

Hailwood


People also ask

How do I figure out the percentage of a number?

Percentage can be calculated by dividing the value by the total value, and then multiplying the result by 100. The formula used to calculate percentage is: (value/total value)×100%.

What is the HTML code for calculating percentage?

In this case, two functions, percentage 1() and percentage 2(), are declared and passed to the button via the onclick event. As a result, when the Calculate button is pressed, the result is displayed.


2 Answers

var result = (35.8 / 100) * 10000; 

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

like image 138
alex Avatar answered Sep 21 '22 17:09

alex


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)); 
like image 28
alcoholtech Avatar answered Sep 25 '22 17:09

alcoholtech