Percentage
#1

Hey,
So well, I'm confused with percentage since I sucks with math,
I'm trying to get 3% from a int, but it's giving 0.
Screen: http://i.imgur.com/aGDs9lg.png

Code of one of the taxes: GeneralTax = GeneralTax+(PayCheck/100)*5;

Full code:
PHP код:
stock RunPaycheck() {
    for(new 
playerid 0playerid MAX_PLAYERSplayerid++) {    
        if(
IsPlayerConnected(playerid)) {
            
//==[PayCheck]==//
            
new PayCheck;
            if(
Player[playerid][Job] == 2//Mechanic
                
PayCheck random(110-90)+90;
            else if(
Player[playerid][Job] == 3//Miner
                
PayCheck random(150-140)+140;
            else
                
PayCheck random(50-45)+45;
            new 
paycheck[128];
            
format(paycheck128"Paycheck: $%i"PayCheck);
            
//==[Tax]==//
            
new GeneralTax;
            
GeneralTax GeneralTax+(PayCheck/100)*5;
            new 
generaltax[128];
            
format(generaltax128"General Tax: -$%i"GeneralTax);
            
//==[Houses]==//
            
new HouseTaxHousesEx;
            for(new 
hid 0hid 500hid++) {
                if(!
isnull(Houses[hid][Owner]) && !strcmp(Houses[hid][Owner], GetPlayerNameEx(playerid))) {
                    
HouseTax = (PayCheck/100) * 3;
                    
HousesEx++;
                }
            }
            new 
housetax[128];
            
format(housetax128"House Tax: -$%i"HouseTax);
            
//==[Business]==//
            
new BusinessTaxBusiness;
            for(new 
bid 0bid 500bid++) {
                if(!
isnull(Stores[bid][Owner]) && !strcmp(Stores[bid][Owner], GetPlayerNameEx(playerid))) {
                    
BusinessTax = (PayCheck/100) * 3;
                    
Business++;
                }
            }
            new 
businesstax[128];
            
format(businesstax128"Business Tax: -$%i"BusinessTax);
            
//==[Result]==//
            
new Result;
            
Result PayCheck-BusinessTax-HouseTax-GeneralTax;
            new 
result[128];
            if(
floatround(Resultfloatround_round) < 0)
                
format(result128"Result: -$%i"Result);
            else 
                
format(result128"Result: $%i"Result);
            
//==[Balance]==//
            
new Balance[128];
            
format(Balance128"New Balance: $%i Old Balance: $%i"Result+Player[playerid][Bank], Player[playerid][Bank]);
            
//==[Messages]==//
            
SendClientMessage(playerid0xD1D1D1FF"_______________[Paycheck]_______________");
            
SendClientMessage(playerid0xD1D1D1FFgeneraltax);
            if(
HousesEx 0)
                
SendClientMessage(playerid0xD1D1D1FFhousetax);
            if(
Business 0)
                
SendClientMessage(playerid0xD1D1D1FFbusinesstax);
            
SendClientMessage(playerid0xD1D1D1FFpaycheck);
            
SendClientMessage(playerid0xD1D1D1FFresult);
            
SendClientMessage(playerid0xD1D1D1FFBalance);
            
SendClientMessage(playerid0xD1D1D1FF"________________________________________");
            
//==[PayCheck]==//
            
Player[playerid][Bank] = Result+Player[playerid][Bank];
        }
    }
    
GameTextForAll("~g~PayDay"80001);

Reply
#2

Don't forget that the result is a floating-point number so round its value downwards.

pawn Код:
GeneralTax = floatround(PayCheck * 0.05, floatround_floor);
You can also multiply it with 0.05 directly to avoid division. For your example, the paycheck is 45 so the 5% of it is 2.25 which makes GeneralTax to be 2.
Reply
#3

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
Don't forget that the result is a floating-point number so round its value downwards.

pawn Код:
GeneralTax = floatround(PayCheck * 0.05, floatround_floor);
You can also multiply it with 0.05 directly to avoid division. For your example, the paycheck is 45 so the 5% of it is 2.25 which makes GeneralTax to be 2.
Thanks mate, Been looking for something like this
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)