AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   Number Formatting (https://forums.alliedmods.net/showthread.php?t=315084)

wOOw 03-21-2019 10:03

Number Formatting
 
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;
}


fysiks 03-21-2019 21:09

Re: Number Formatting
 
Bugsy wrote this function already and it was posted here. I'm quite sure that Bugsy's will be more efficient.

Also, you shouldn't be returning a string, strings should be pass by reference as it is done in function to which I linked.

bigdaddy424 03-12-2023 14:44

Re: Number Formatting
 
i came across this post while trying to fix something that bothered me days ago
i googled ways to avoid zeros after float numbers and seems like `%g` is not embedded in pawn and a function i did returns something like this
https://i.ibb.co/Tb38hZz/image.png
i was going to use your thing but some zeros wont hurt after looking the mess you put together
Code:
#include <amxmodx> enum PValueData{     Number,     Place_Value[2] } new PValue[][PValueData] = {        { 1000000000, "B"},           { 1000000, "M"},              { 1000, "K"} } public plugin_init()     register_srvcmd("f", "srvcmd_format") public srvcmd_format(){     new arg[16], num     read_args(arg, charsmax(arg))     num = str_to_num(arg)     server_print(format_number(num)) } public format_number(number){     new abs_number = abs(number)     for (new i = 0; i < sizeof(PValue); i++){         if (abs_number >= PValue[i][Number]){             return fmt("%s%.2f %s", number < 0 ? "-" : "", float(abs_number) / float(PValue[i][Number]), PValue[i][Place_Value])         }     }     return fmt("%s%d", number < 0 ? "-" : "", abs_number) }

georgik57 04-07-2023 06:04

Re: Number Formatting
 
I also have one but mine only supports integers and only adds commas:

PHP Code:

ftNumberAddCommasI(iNumberszNumber[14] = "", const iSize charsmax(szNumber))
{
    
num_to_str(iNumberszNumberiSize);
    
    if (-
1000 iNumber 1000)
        return 
szNumber;
    
    for (
iNumber strlen(szNumber) - 3iNumber > ((szNumber[0] != '-') ? 1); iNumber -= 3)
        
format(szNumber[iNumber], charsmax(szNumber) - iNumber 1",%s"szNumber[iNumber]);
    
    return 
szNumber;



Napoleon_be 04-14-2023 12:12

Re: Number Formatting
 
I'm using something in python that might be useful here:

PHP Code:

def convert_size(size_bytes):
   if 
size_bytes == 0:
       return 
"0B"
   
size_name = ("B""KB""MB""GB""TB""PB""EB""ZB""YB")
   
int(math.floor(math.log(size_bytes1024)))
   
math.pow(1024i)
   
round(size_bytes p2)
   return 
"%s%s" % (ssize_name[i]) 



All times are GMT -4. The time now is 10:51.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.