Lasso Soft Inc. > Home

[lp_display_filesize]

Linklp_display_filesize
AuthorBil Corry
CategoryUtility
Version8.5.x
LicensePublic Domain
Posted02 Sep 2006
Updated02 Sep 2006
More by this author...

Description

Returns the filesize formatted for display given the number of bytes.

Requires [lp_decimal_float] [lp_decimal_fixed]

Sample Usage

lp_display_filesize: 2023940;'
'; lp_display_filesize: 2023940, -kb;'
'; Returns: 1.93 MB 1,977 KB

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.

[

define_tag:'lp_display_filesize',
	-description='Returns the filesize formatted for display given the number of bytes.',
	-priority='replace',
	-required='filesize',
	-optional='kb', 	// returns everything in kilobytes
	-encodenone;
	
	// based on a tag from Jason Huck
	// http://tagswap.net/bytes_format

	// and Pier Kuipers
	// http://tagswap.net/PK_ByteSize
	
	local:
		'bytes'		= decimal: #filesize,
		'blocksize'	= 1024.0,
		'exp'		= 1,
		'units'		= (:'B ','KB','MB','GB','TB');
	
	if: (local_defined:'kb');
		if: (lp_decimal_float: #bytes / #blocksize) == 0;
			local:'return' = (lp_decimal_fixed: #bytes / #blocksize);
		else;
			local:'return' = ((lp_decimal_fixed: #bytes / #blocksize) + 1);
		/if;
		#return->(setformat: -groupchar=',');
		return: #return ' KB';
	/if;
	
	#bytes->(setformat: -precision=2);
	
	while: true;
		if: #bytes < #blocksize;
			return: (#exp <= 2 ? (integer:#bytes) | #bytes) ' ' #units->(get:#exp);
		else; // >=
			#bytes /= #blocksize;
			#exp += 1;
		/if;
	/while;

/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