tags:

views:

974

answers:

5

I want to calculate what is $x percentage of a $total. $x could be 15%, 20%, etc, and $total could be 1000, 2000, etc. So I'd want the 15% of 1000, for example.

What is the formula for calculating this? (I know this isn't exactly a coding question but I'm coding this feature and need help!

+3  A: 

Given X and Y, X% of Y is X * Y/100. If using integer arithmetic, make sure you do (X * Y)/100, not (X / 100) * Y.

15% of 1000 is (15*1000)/100, which is 150.

Airsource Ltd
+1  A: 

formulas:

percentage = partialAmount / totalAmount
totalAmount = partialAmount / percentage
partialAmount = percentage * totalAmount

note that percentage is normally a decimal number between 0 and 1

Steven A. Lowe
Which always amounts to 0 in integer arithmetic.
Airsource Ltd
@[Airsource Ltd]: well that would be a silly way to try to calculate percentages, now wouldn't it?
Steven A. Lowe
percentage is normally a number between 0 and 100. Its meaning is per hundred anyway.
Szere Dyeri
@[Szere Dyeri]: percentage is displayed as 0-100, but calculated as 0-1 decimal. At least in every programming language I've ever used ;-)
Steven A. Lowe
+1  A: 

Don't know if I'm missing something here but if you just want the percentage of a number it's just multiply it by the percentage and divide by 100:

NewTotal = Total * Percentage / 100

IE:

NewTotal = 1000 * 15/100

or

NewTotal = 1000 * 0.15

DaEagle
+10  A: 
(actual / available) * 100 = percent // start

actual / available = percent / 100

actual = (percent / 100) * available // finish

E.g. 15% of 1000

actual = (15 / 100) * 1000
actual = 0.15 * 1000
actual = 150
Jonathan Lonowski
It was the third of these I was after :)
Of course. But, "show your work." :P
Jonathan Lonowski
What do you mean? You want to see the feature where I was doing this?
It's an old school joke ...
BobbyShaftoe
What does the joke mean?
Oops...nvm. It was a math class joke. But, I made the same assumption that it was homework. Sorry. ;)
Jonathan Lonowski
That's ok, but I wanna know what the joke means?
The joke was just in the reference. It's a common instruction for math classes so students write down every step they took to solve an algebraic problem -- not just the solution.
Jonathan Lonowski
A: 

Though this particular question has been answered, you may find this site very helpful for basic algebra topics.

http://www.purplemath.com/modules/index.htm

BobbyShaftoe