SA-MP Forums Archive
Rounding numbers - 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: Rounding numbers (/showthread.php?tid=298400)



Rounding numbers - Luis- - 20.11.2011

Hi, I am planning a bank system for my server and I would like to know how I can make it so when a player goes to withdraw it'll say his balance is, example:
Your balance is $542, today you may withdraw $500.

Is it possible, if so how would it be done?


Re: Rounding numbers - .:Kaos:. - 20.11.2011

Would read this, might help. https://sampwiki.blast.hk/wiki/Floatround


AW: Rounding numbers - Babul - 20.11.2011

Код:
new BalanceRounder=100;

new BalanceRoundedDown=Balance-(Balance%BalanceRounder);



Re: Rounding numbers - Vince - 20.11.2011

If you only want the player to withdraw multipliers of 100, this may work:
pawn Код:
new
    multiplier = (balance - (balance % 100)) / 100),
    maxamount = (balance >= 100) ? multiplier * 100 : balance;



Re: Rounding numbers - Ash. - 20.11.2011

Quote:
Originally Posted by -Luis
Посмотреть сообщение
Hi, I am planning a bank system for my server and I would like to know how I can make it so when a player goes to withdraw it'll say his balance is, example:
Your balance is $542, today you may withdraw $500.

Is it possible, if so how would it be done?
Convert the number to a string, get the first character (in the 542 example, it would be 5), then get the length of the string (542 example = 3), take 1 from the length (length is now 2) and add that many 0's to the end. Something like:

pawn Код:
stock bankRound(number)
{
     new
          szNumStr[10],
          iLen
     ;
     format(szNumStr, sizeof(szNumStr), "%i", number);
     iLen = strlen(szNumStr);
     format(szNumStr, sizeof(szNumStr), "%s", szNumStr[0]);
     for(new i; i < (iLen - 1); i++) format(szNumStr, sizeof(szNumStr), "%s0", szNumStr);
     return strval(szNumStr);
}
Theres probably a MUCH better way to do this, but if you were to use floatround (example), if it were >=n50 (where n is a preceding integer) it would round UP not down.


Re: Rounding numbers - AndreT - 20.11.2011

Quote:
Originally Posted by Vince
Посмотреть сообщение
If you only want the player to withdraw multipliers of 100, this may work:
pawn Код:
new
    multiplier = (balance - (balance % 100)) / 100),
    maxamount = (balance >= 100) ? multiplier * 100 : balance;
Thats one rounded too much on the first line.

Use Babul's method, you could say it is much faster than operating with strings.


Re: Rounding numbers - Luis- - 20.11.2011

Right, i'm using Babul's method and it's working perfectly, thanks everyone!


Re: Rounding numbers - Finn - 20.11.2011

Edit: nvm