CFLib.org – Common Function Library Project

convertMemoryUnit(num, fromUnit, toUnit [, format])

Last updated May 9, 2009

Version: 0 | Requires: ColdFusion 5 | Library: StrLib

 
Rated 0 time(s). Average Rating: 0

Description:
Convert any number between the following units of memory: bit, byte, kilobit, kilobyte, megabit, megabyte, gigabit, gigabyte, terabit, terabyte, petabit, petabyte, exabit, exabyte, zettabit, zettabyte, yottabit, and yottabyte. Can also specify number format to return.

Return Values:
returns a string

Example:

view plain print about
<cfset kb = convertMemoryUnit(1024,"byte","kilobyte") />

Parameters:

Name Description Required
num number to convert Yes
fromUnit unit to convert from Yes
toUnit unit to convert to Yes
format format to return number in No

Full UDF Source:

view plain print about
<!---
 Converts a unit of memory to another unit of memory.
 
 @param num      number to convert (Required)
 @param fromUnit      unit to convert from (Required)
 @param toUnit      unit to convert to (Required)
 @param format      format to return number in (Optional)
 @return returns a string 
 @author Chris Schofield (chris@chrisschofield.com) 
 @version 0, May 9, 2009 
--->

<cffunction name="convertMemoryUnit" access="public" returntype="string">
    <!--- Number to convert. CMS --->
    <cfargument name="num"         required="true" type="numeric" />
    <!--- Memory unit of arguments.num (byte, kilobyte, etc.). CMS --->
    <cfargument name="fromunit"    required="true" type="string" />
    <!--- Memory to convert to. CMS --->
    <cfargument name="tounit"     required="true" type="string" />
    <!--- Number format to return (9.99, 9.99999, etc.). CMS --->
    <cfargument name="format"     default="9.9"    type="string" />

    <!--- Convert the number to bytes. CMS --->
    <cfset var bytes = 0 />
    <cfset var conv = 0 />

    <!--- Then convert to the specified unit. CMS --->
    <cfswitch expression="#arguments.fromunit#">
        <cfcase value="bit">             <cfset bytes = (arguments.num/8) />         </cfcase>
        <cfcase value="byte">             <cfset bytes = arguments.num />             </cfcase>
        <cfcase value="kilobit">         <cfset bytes = (arguments.num*(2^10)/8) />    </cfcase>
        <cfcase value="kilobyte">         <cfset bytes = (arguments.num*(2^10)) />    </cfcase>
        <cfcase value="megabit">         <cfset bytes = (arguments.num*(2^20)/8) />    </cfcase>
        <cfcase value="megabyte">         <cfset bytes = (arguments.num*(2^20)) />    </cfcase>
        <cfcase value="gigabit">         <cfset bytes = (arguments.num*(2^30)/8) />    </cfcase>
        <cfcase value="gigabyte">         <cfset bytes = (arguments.num*(2^30)) />    </cfcase>
        <cfcase value="terabit">         <cfset bytes = (arguments.num*(2^40)/8) />    </cfcase>
        <cfcase value="terabyte">         <cfset bytes = (arguments.num*(2^40)) />    </cfcase>
        <cfcase value="petabit">         <cfset bytes = (arguments.num*(2^50)/8) />    </cfcase>
        <cfcase value="petabyte">         <cfset bytes = (arguments.num*(2^50)) />    </cfcase>
        <cfcase value="exabit">         <cfset bytes = (arguments.num*(2^60)/8) />    </cfcase>
        <cfcase value="exabyte">         <cfset bytes = (arguments.num*(2^60)) />    </cfcase>
        <cfcase value="zettabit">         <cfset bytes = (arguments.num*(2^70)/8) />    </cfcase>
        <cfcase value="zettabyte">         <cfset bytes = (arguments.num*(2^70)) />    </cfcase>
        <cfcase value="yottabit">         <cfset bytes = (arguments.num*(2^80)/8) />    </cfcase>
        <cfcase value="yottabyte">         <cfset bytes = (arguments.num*(2^80)) />    </cfcase>
        <cfdefaultcase>
            <cfthrow message="Could not convert #arguments.num# to #arguments.fromunit#."
                    detail="Unit #arguments.fromunit# is not supported." />

        </cfdefaultcase>
    </cfswitch>

    <!--- First convert to bytes. CMS --->
    <cfswitch expression="#arguments.tounit#">
        <cfcase value="bit">         <cfset conv = (bytes*8) />             </cfcase>
        <cfcase value="byte">         <cfset conv = bytes />                 </cfcase>
        <cfcase value="kilobit">     <cfset conv = (bytes/(2^10)*8) />    </cfcase>
        <cfcase value="kilobyte">     <cfset conv = (bytes/(2^10)) />        </cfcase>
        <cfcase value="megabit">     <cfset conv = (bytes/(2^20)*8) />    </cfcase>
        <cfcase value="megabyte">     <cfset conv = (bytes/(2^20)) />        </cfcase>
        <cfcase value="gigabit">     <cfset conv = (bytes/(2^30)*8) />    </cfcase>
        <cfcase value="gigabyte">     <cfset conv = (bytes/(2^30)) />        </cfcase>
        <cfcase value="terabit">     <cfset conv = (bytes/(2^40)*8) />    </cfcase>
        <cfcase value="terabyte">     <cfset conv = (bytes/(2^40)) />        </cfcase>
        <cfcase value="petabit">     <cfset conv = (bytes/(2^50)*8) />    </cfcase>
        <cfcase value="petabyte">     <cfset conv = (bytes/(2^50)) />        </cfcase>
        <cfcase value="exabit">     <cfset conv = (bytes/(2^60)*8) />    </cfcase>
        <cfcase value="exabyte">     <cfset conv = (bytes/(2^60)) />        </cfcase>
        <cfcase value="zettabit">     <cfset conv = (bytes/(2^70)*8) />    </cfcase>
        <cfcase value="zettabyte">     <cfset conv = (bytes/(2^70)) />        </cfcase>
        <cfcase value="yottabit">     <cfset conv = (bytes/(2^80)*8) />    </cfcase>
        <cfcase value="yottabyte">     <cfset conv = (bytes/(2^80)) />        </cfcase>
        <cfdefaultcase>
            <cfthrow message="Could not convert #arguments.num# to #arguments.tounit#."
                    detail="Unit #arguments.tounit# is not supported." />

        </cfdefaultcase>
    </cfswitch>

    <!--- Then convert the bytes to the tounit. CMS --->
    <cfreturn NumberFormat(conv,arguments.format) />
</cffunction>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
11 day(s) ago

Will Belden Will Belden added
longTime
17 day(s) ago

James Sleeman James Sleeman added
quickSort
27 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
30 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson