Raised This Month: $12 Target: $400
 3% 

Number Formatting


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
wOOw
Member
Join Date: Jul 2015
Location: Lithuania
Old 03-21-2019 , 10:03   Number Formatting
Reply With Quote #1

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;
}
__________________

Last edited by wOOw; 03-21-2019 at 17:50.
wOOw is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 03-21-2019 , 21:09   Re: Number Formatting
Reply With Quote #2

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.
__________________

Last edited by fysiks; 03-21-2019 at 21:11.
fysiks is offline
bigdaddy424
Senior Member
Join Date: Oct 2021
Location: Jupiter
Old 03-12-2023 , 14:44   Re: Number Formatting
Reply With Quote #3

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

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) }
__________________

Last edited by bigdaddy424; 04-07-2023 at 22:02. Reason: works w/ negative numbers + missing = before >
bigdaddy424 is offline
georgik57
Veteran Member
Join Date: Oct 2008
Location: 🎧Music World
Old 04-07-2023 , 06:04   Re: Number Formatting
Reply With Quote #4

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;

__________________
georgik57 is offline
Send a message via MSN to georgik57 Send a message via Yahoo to georgik57 Send a message via Skype™ to georgik57
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 04-14-2023 , 12:12   Re: Number Formatting
Reply With Quote #5

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]) 
__________________
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 16:22.


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