30.03.2016, 05:42
pawn Код:
formatInt(intVariable, iThousandSeparator = ',', iCurrencyChar = '$')
{
static
s_szReturn[ 32 ],
s_szThousandSeparator[ 2 ] = { ' ', EOS },
s_szCurrencyChar[ 2 ] = { ' ', EOS },
s_iVariableLen,
s_iChar,
s_iSepPos,
bool:s_isNegative
;
format( s_szReturn, sizeof( s_szReturn ), "%d", intVariable );
if(s_szReturn[0] == '-')
s_isNegative = true;
else
s_isNegative = false;
s_iVariableLen = strlen( s_szReturn );
if ( s_iVariableLen >= 4 && iThousandSeparator)
{
s_szThousandSeparator[ 0 ] = iThousandSeparator;
s_iChar = s_iVariableLen;
s_iSepPos = 0;
while ( --s_iChar > _:s_isNegative )
{
if ( ++s_iSepPos == 3 )
{
strins( s_szReturn, s_szThousandSeparator, s_iChar);
s_iSepPos = 0;
}
}
}
if(iCurrencyChar) {
s_szCurrencyChar[ 0 ] = iCurrencyChar;
strins( s_szReturn, s_szCurrencyChar, _:s_isNegative );
}
return s_szReturn; // ALL credits go to Slice
}
pawn Код:
[30/03/2016 01:46:55] formatted: $3
[30/03/2016 01:46:55] formatted: -$3
[30/03/2016 01:46:55] formatted: $33
[30/03/2016 01:46:55] formatted: -$33
[30/03/2016 01:46:55] formatted: $338
[30/03/2016 01:46:55] formatted: -$338
[30/03/2016 01:46:55] formatted: $3,382
[30/03/2016 01:46:55] formatted: -$3,382
[30/03/2016 01:46:55] formatted: $33,822
[30/03/2016 01:46:55] formatted: -$33,822
[30/03/2016 01:46:55] formatted: $338,227
[30/03/2016 01:46:55] formatted: -$338,227
[30/03/2016 01:46:55] formatted: $3,382,273
[30/03/2016 01:46:55] formatted: -$3,382,273
[30/03/2016 01:46:55] formatted: $33,822,736
[30/03/2016 01:46:55] formatted: -$33,822,736
pawn Код:
new i = 0;
while(i < 210000000)
{
i += random(9) + 1;
printf("formatted: %s", formatInt(i));
printf("formatted: %s", formatInt(i * -1));
i *= 10;
}
