Name

FormatFileSize

Original Contributor

Originally contributed by Matt Bramer (@IOnline247) and adapted by Marc D Anderson (@sympmarc).

Purpose

This template will format a file size (or any other number for that matter) into the appropriate unit of measure (Bytes, KB, MB, GB, TB) based on its magnitude. The template uses traditional conversion factors (powers of 2) rather than the more recent standard (powers of 10). See http://en.wikipedia.org/wiki/Megabyte.

Example

<xsl:call-template name="FormatFileSize"> 
   <xsl:with-param name="FileSize" select="@File_x0020_Size" /> 
</xsl:call-template>

Code

<xsl:template name="FormatFileSize"> 
    <xsl:param name="FileSize" /> 
    <xsl:choose> 
        <xsl:when test="$FileSize &gt; 1099511627776"> 
           <xsl:value-of select="concat(format-number($FileSize div 1099511627776, '###.#'), 'TB')"/> 
        </xsl:when> 
        <xsl:when test="$FileSize &gt; 1073741824"> 
            <xsl:value-of select="concat(format-number($FileSize div 1073741824, '###.#'), 'GB')"/> 
        </xsl:when> 
        <xsl:when test="$FileSize &gt; 1048576"> 
            <xsl:value-of select="concat(format-number($FileSize div 1048576, '###.#'), 'MB')"/> 
        </xsl:when> 
        <xsl:when test="$FileSize &gt; 1024"> 
            <xsl:value-of select="concat(format-number($FileSize div 1024, '###.#'), 'KB')"/> 
        </xsl:when> 
        <xsl:otherwise> 
            <xsl:value-of select="concat(format-number($FileSize, '###.#'), 'Bytes')"/>    
        </xsl:otherwise> 
     </xsl:choose> 
</xsl:template>

Last edited Jul 21, 2011 at 4:40 PM by sympmarc, version 8

Comments

No comments yet.