Lasso Soft Inc. > Home

[mv_fmtnum]

Linkmv_fmtnum
AuthorMarc Vos
CategoryTags
Version9.x
LicensePublic Domain
Posted22 May 2013
Updated22 May 2013
More by this author...

Description

Formats a number, with or without characters via a mask, to a complete string.

 

Sample Usage

mv_fmtnum(-number=123456, -mask='S/N ########')	
'S/N 12345600'
mv_fmtnum(-number=123456, -mask='S/N ########', -direction='')	
'S/N 12345600'
mv_fmtnum(-number=123456, -mask='S/N ########', -direction='R')	
'S/N 00123456'
mv_fmtnum(-number=-123456, -mask='S/N ########', -direction='R')	
'S/N 00123456-'
mv_fmtnum(-number=-123456, -mask='S/N ########', -direction='L')	
'-S/N 12345600'
mv_fmtnum(-number='123456', -mask='$$-$$$/$$$', -direction='L')	
'12-345/6  '
mv_fmtnum(-number='-123456', -mask='##-$$$/$$$', -direction='R')	
'00-123/456-'
mv_fmtnum(-number=1234.56, -mask='%%%%%%%%%.####', -direction='L')	
'123456.0000'
mv_fmtnum(-number=-1234.56, -mask='%%%%%%%%%.##%%', -direction='R')	
'12.3456-'
mv_fmtnum(-number='0.56', -mask='%%%%%%%%.####', -direction='.')	
'0.5600'
mv_fmtnum(-number=-0.6, -mask='%%%%%%%%,##%%', -direction=',')	
'0,60-'
mv_fmtnum(-number='1234,56', -mask='$$$$$$$#.%%%%', -direction='.')	
'    1234.56'
mv_fmtnum(-number='-12345,6', -mask='########.####', -direction='.')	
'00012345.6000-'
mv_fmtnum(-number=123456, -mask='$$$$$$$#,%%%%', -direction=',')	
'  123456'
mv_fmtnum(-number=123456, -mask='%%%%%%%#.##%%', -direction='.')	
'123456.00'
mv_fmtnum(-number=1234567890, -mask='%%%%.%%%%.%%%#.##%%', -direction='L')	
'1234.5678.900.00'
mv_fmtnum(-number=1234567890, -mask='%%%%.%%%%.%%%#.##%%', -direction='R')	
'.12.3456.7890'
mv_fmtnum(-number=1234567890, -mask='%%%%.$$$$.$$$$.$$$$', -direction='L')	
'1234.5678.90  .    '
mv_fmtnum(-number=1234567890, -mask='%%%%.$$$$.$$$$.$$$$', -direction='R')	
'.  12.3456.7890'
mv_fmtnum(-number=200.000000000, -mask='%%%%%%%%%#,%%%%%%%%%', -direction=',')	
'200'

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.

[
/*
	Formats a number into a string using a mask where the 
		# is a position for a digit or character. When no numeric positions are left, the # is zero-filled. 0.56 with '####,##' would result in '0000,56'.
		$ is a position for a digit or character. When no numeric positions are left, the $ is blanks-filled. 0.56 with '$$$#,##' would result in '   0,34'.
		% is a position for a digit or character. When no numeric positions are left, the % is empty-filled. 0.56 with '%%%#,##' would result in '0,34'.

	Timestamp example mask : ####-##-## ##:##:##
	
	Direction can be
		L(eft)	- Numbers are placed in the mask starting from the left.
		R(ight)	- Numbers are placed in the mask starting from the right.
		, or .	- Fixes the number of decimal places. The number of decimal places is adjusted to what the mask dictates. Numbers are placed in the mask starting from the right.
			'123'		and '###.##' -> '123.00'
			'123.4'		and '###.##' -> '123.40'
			'123.45'	and '###.##' -> '123.45'
			'123.456'	and '###.##' -> '123.46'
			'123.4'		and '###.%%' -> '123.4'
			'123'		and '###.%%' -> '123'
*/
define_tag('mv_fmtnum', -required='number', -copy, -required='mask', -copy, -optional='direction', -copy);
	local('result'='', 'num'='', 'tmp'='', 'pos'=0, 't'='', 'mleft'='', 'mright'='', 'nleft'='', 'nright'='', 'negate'='', 'flag'=false);
	
	error_reset;
	
	if(! local_defined('direction'));
		local('direction') = 'L';
	else('RL,.' !>> #direction);
		#direction = 'L';
	/if;

	(decimal(#number) < 0) ? #negate = true | #negate = false;
	
	// Check if Lasso will add decimal positions
	if(#number->isa('decimal'));
		#number = string(#number);	// Make it a string
		#number->removetrailing('0');
	else;
		#number = string(#number);	// Make it a string
	/if;
	
	if((#direction == ',') || (#direction == '.'));
		// Locate . or , in mask
		#tmp='.';
		#pos = #mask->find(#tmp);
		if(#pos == 0);
			#tmp=',';
			#pos = #mask->find(#tmp);
		/if;

		// Split mask in two parts : number + fractional
		if(#pos == 0);
			#mleft = #mask;
			#mright = '';
		else;
			#mleft = #mask->split(#tmp)->get(1);
			#mright = #mask->split(#tmp)->get(2);
		/if;
		
		// Locate . or , in number
		#tmp='.';
		#pos = #number->find(#tmp);
		if(#pos == 0);
			#tmp=',';
			#pos = #number->find(#tmp);
		/if;

		// Split number in two parts : number + fractional
		if(#pos == 0);
			#nleft = #number;
			#nright = '';
		else;
			#nleft = #number->split(#tmp)->get(1);
			#nright = #number->split(#tmp)->get(2);
		/if;

		// Compare decimal places between mask and number. Adjust fractional part of number to match mask.
		if(#mright == '');
			#nright = '';
		else(#mright->size > #nright->size);
			#nright += string('00000000000')->substring(1, (#mright->size - #nright->size));
		else(#nright->size > #mright->size);
			#nright = lp_decimal_precisionSet('0.' + #nright, #mright->size);
			#nright = string(#nright)->substring(3, #mright->size);
		/if;
		
		// Construct new number
		#number = #nleft + '.' + #nright;
		#flag = true;
	/if;

	// get only the characters and numbers
	loop(#number->size);
		#tmp = #number->get(loop_count);
		if(((#tmp >= '0') && (#tmp <= '9')) || ((#tmp >= 'a') && (#tmp <= 'z')) || ((#tmp >= 'A') && (#tmp <= 'Z')));
			#num = #num + #tmp;
		/if;
	/loop;

	if(#direction == 'L');
		#pos = 1;
		loop(#mask->size);
			#t = #mask->get(loop_count);
			if('#$%' >> #t);
				if(#pos <= #num->size);
					#result += #num->get(#pos);
				else;
					if(#t == '$');
						#result += ' ';
					else(#t == '%');
						#result += '';
					else;
						#result += '0';
					/if;
				/if;
				#pos += 1;
			else;
				#result += #mask->get(loop_count);
			/if;
		/loop;
	else;
		#pos = #num->size;
		loop(-from=#mask->size, -to=1, -by=(-1));
			#t = #mask->get(loop_count);
			if('#$%' >> #t);
				if((#pos <= #num->size) && (#pos > 0));
					if(#num->get(#pos)=='0');
						if(! #flag || #t != '%');
							#flag = false;
							#result = #num->get(#pos) + #result;
						/if;
					else;
						#flag = false;
						#result = #num->get(#pos) + #result;
					/if;
				else;
					if(#t == '$');
						#flag = false;
						#result = ' ' + #result;
					else(#t == '%');
						#result = '' + #result;
					else;
						#flag = false;
						#result = '0' + #result;
					/if;
				/if;
				#pos -= 1;
			else(! #flag || (#flag && #t != #direction));
				#result = #mask->get(loop_count) + #result;
			/if;
		/loop;
	/if;
	
	if(#negate == true);
		(#direction == 'L') ? #result = '-' + #result | #result += '-';
	/if;
		
	return(#result);
/define_tag;
]

Related Tags

Comments

No comments

Please log in to comment

Subscribe to the LassoTalk mail list

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft