Percentage Problem - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Percentage Problem (
/showthread.php?tid=494321)
Percentage Problem -
Problems - 12.02.2014
I use /rob command to rob 10% of the other player's money
pawn Код:
new mrand =random((GetPlayerMoney(ID)/100)*10);
GivePlayerMoney(playerid,mrand);
but when the other player got 700K it robbed about 600K
Re: Percentage Problem -
CuervO - 12.02.2014
The formula to get any percentage is, generally speaking,
A % - B
C % - D
Whereas D is the incognita:
(C * B) / A = D (always divide by the number that's opposite to the incognita)
100 % - GetPlayerMoney(ID)
10% - X
(10 * GetPlayerMoney(ID)) / 100 = X
That's what you should use.
Re: Percentage Problem -
PowerPC603 - 12.02.2014
10 percent is 1/10th, so you can use this:
pawn Код:
new mrand = random(GetPlayerMoney(ID)/10);
GivePlayerMoney(playerid,mrand);
Re: Percentage Problem -
CuervO - 12.02.2014
Quote:
Originally Posted by PowerPC603
10 percent is 1/10th, so you can use this:
pawn Код:
new mrand = random(GetPlayerMoney(ID)/10); GivePlayerMoney(playerid,mrand);
|
Of course he can also multiply the number by 0.1 to get the 10% of said number, but your formula would fail if he needed the 11%, since he can't divide by 11 to get 11%.
By using a three rule function you will get the exact plus you learn to do something new and might use it in the future. Alternatively you can always multiply (less than 1 to get the percentage, more than 1 to sum X percentage to the original number).
0.1 = 10%
0.11 = 11%
0.5 = 50%
0.9 = 90%
0.05 = 5%
1 = 100%
1.1 = 110%
1.5 = 150%
2 = 200%
and so on..