calculating 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: calculating problem (
/showthread.php?tid=614524)
calculating problem -
Airblog - 09.08.2016
Hi
i have a problem
i want to make a skill percentage according to player kills and player deaths
this is my code i think all should work correctly but it just shows 0%
Код:
GetPlayerLevel(playerid)
{
new asb,Shotor,agoz,bgoz,sumi,aall;
asb = PlayerInfo[playerid][pKills];
Shotor = PlayerInfo[playerid][pDeads];
agoz = asb - Shotor;
bgoz = asb + Shotor;
sumi = agoz / bgoz;
aall = sumi * 100;
return aall;
}
whats the problem?
Re: calculating problem -
[cS]Owain - 09.08.2016
The variable are of int (integer) type which do not store decimal places. Thus when you do "sumi = agoz / bgoz;", the answer is probably in decimals which gets truncated to 0 always as it is integer type.
Declare 'aaall' and 'sumi' as float.
Like this :
Quote:
new asb,Shotor,agoz,bgoz,Float:sumi,Float:aall;
|
Re: calculating problem -
SyS - 09.08.2016
you have to give a check whether agoz or bgoz is greater and do the division because an integer division having result having less than 1 is round to 0 and the result will be zero.
PHP код:
GetPlayerLevel(playerid)
{
new asb,Shotor,agoz,bgoz,sumi,aall;
asb = PlayerInfo[playerid][pKills];
Shotor = PlayerInfo[playerid][pDeads];
agoz = asb - Shotor;
bgoz = asb + Shotor;
if(agoz>bgoz)
sumi = agoz / bgoz;
else
sumi = bgoz / agoz;
aall = sumi * 100;
return aall;
}
or declaring the vars as floats can avoid it.
Re: calculating problem -
PrO.GameR - 09.08.2016
If only you didn't use animals and fart as variable names

If it's a percent, you need to use float-float divisions to work it out.
new Float

umi = float(agoz)/float(bgoz) *100.0;
or you could do agoz*100 / bgoz, which still give a percent, but with a margin of error.
Re: calculating problem -
Airblog - 09.08.2016
Quote:
Originally Posted by PrO.GameR
If only you didn't use animals and fart as variable names 
If it's a percent, you need to use float-float divisions to work it out.
new Float  umi = float(agoz)/float(bgoz) *100.0;
or you could do agoz*100 / bgoz, which still give a percent, but with a margin of error.
|
thanks buddy

+Rep