Rounding numbers
#1

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?
Reply
#2

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

Код:
new BalanceRounder=100;

new BalanceRoundedDown=Balance-(Balance%BalanceRounder);
Reply
#4

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;
Reply
#5

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.
Reply
#6

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.
Reply
#7

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

Edit: nvm
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)