Problem with integers and gettings percentages from them
#1

Hello there,

I've been working on a paycheck script and I want to give a user a certain percentage interest on his balance when a paycheck happens.
With a start bank balance of 10000 the script works 2 times. With a percentage of 1% the player will get 100 on his first paycheck, and then 101. But after that; the server keeps giving 101 interest.

My guess is that the interest integer tries to get 1% of 10201 which then fails since it has multiple decimals.
How can I fix this problem?

PHP код:
if(Player[playerid][pBank] > 0)
    {
        new 
interestnewBank;
        
newBank = ((Player[playerid][pBank] / 100) * (100 BANK_DEFAULT_INTEREST));
        
interest newBank Player[playerid][pBank];
        
        
SendClientMessageEx(playerid, -1"Your old balance is %d."Player[playerid][pBank]);
        
SendClientMessageEx(playerid, -1"Your balance interest is %d percent, which gives you %d."BANK_DEFAULT_INTERESTinterest);
        
SendClientMessageEx(playeridCOLOR_SUCCESS"Your new balance is %d."newBank);
        
        
Player[playerid][pBank] = newBank;
        
    } 
Reply
#2

Take note: integer divided by integer remains integer. Any would-be fractional part is discarded. Use floating point arithmetic to do the calculation (this can be as simple as adding .0 to the operands). Then use floatround to save the value to their account.
Reply
#3

PHP код:
newBank = ((Player[playerid][pBank] / 100) * (100 BANK_DEFAULT_INTEREST)); 
As Vince said int*/+-int equals int, a simple add of .0 can turn those constants to float, resulting in a float division, also I would like to keep all the calculation of constants together (as far as I know, they get compiled as 1 number if they are all constants) so
PHP код:
        new Bank = ((Player[playerid][pBank]) * (1.0+(BANK_DEFAULT_INTEREST/100.0)); 
Is actually quite better IMO (Although all of it should be under floatround.)
Reply
#4

Both of you, thanks for the response. Coming from a JavaScript background, the usage of float here feels buggy for me. But I guess I'll have to go with it anyways. Thanks.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)