Simple stock for formating numbers.
Returns number as string.
If only_num bool is set to false it will return formated number with commas ( 10,000 ) if true then with abbreviation ( 10k ).
If number is not round ( 2.3m, 10k, 159k ) it will be formated with commas ( 2,300,001 ).
At the moment it will only format till 999,999,999 after that it will output without commas or abbreviation in string.
Examples:
-------------------------------------------------
format_number(10000, true)
Result
10,000
-------------------------------------------------
format_number(10000, false)
or
format_number(10000)
Result
10k
-------------------------------------------------
format_number(2300000 , true)
Result
2,300,000
-------------------------------------------------
format_number(2300000 , false)
or
format_number(2300000 )
Result
2,3m
-------------------------------------------------
format_number(180390644 , false)
or
format_number(180390644 )
Result
180,390,644
Code:
stock format_number(num, bool:only_num = false)
{
new s_num[16], million[16], thousand[16], hundred[16], szNum[32], p_length;
num_to_str(num,s_num,15);
p_length = strlen(s_num);
switch(p_length)
{
case 1,2,3: formatex(szNum, charsmax(szNum), "%s", s_num);
case 4,5,6:
{
copy(thousand,p_length - 3, s_num[0]);
copy(hundred,3, s_num[p_length - 3]);
if (!only_num) { formatex(szNum, charsmax(szNum), "%s%s%s", thousand, num % 1000 == 0 ? "k" : ",", num % 1000 == 0 ? "" : hundred); }
else { formatex(szNum, charsmax(szNum), "%s,%s", thousand, hundred); }
}
case 7,8,9:
{
copy(million,p_length - 6, s_num[0]);
copy(thousand,3, s_num[p_length - 6]);
copy(hundred,3, s_num[p_length - 3]);
if (!only_num) { formatex(szNum, charsmax(szNum), "%s%s%s%s%s", million, num % 1000000 == 0 ? "" : ",", num % 1000000 == 0 ? "" : thousand, num % 1000000 == 0 ? "" : ",", num % 1000000 == 0 ? "m" : hundred); }
else { formatex(szNum, charsmax(szNum), "%s,%s,%s", million, thousand, hundred); }
}
default : formatex(szNum, charsmax(szNum), "%s", s_num);
}
return szNum;
}
__________________