Raised This Month: $115 Target: $400
 28% 

Number Formatting


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
wOOw
Junior 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
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:45.


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