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)
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%.
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.
var result = (35.8 / 100) * 10000;
(Thank you jball for this change of order of operations. I didn't consider it).
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With