Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Apr 2005
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile XSLT - Convert string - Bytes to KB or MB

    Hiya,

    I'm trying to format a SharePoint webpart.

    I need to convert a string which contains numerical data - the filesize in bytes - into kb or Mb as appropriate. I'm using the following code, but it appears to break SharePoint. I think it's the use of the < and > which is the problem?

    Code:
    &gt;  and &lt;
    Code:
    	<xsl:variable name="FileSizeKb">
    		<xsl:if test="string-length(@FileSizeDisplay) &gt; 0"> 
    			<xsl:if test="number(@FileSizeDisplay) &gt; 0"> 
    				<xsl:choose> 
    					<xsl:when test="round(@FileSizeDisplay div 1024) &lt; 1"><xsl:value-of select="@FileSizeDisplay" /> Bytes</xsl:when> 
    					<xsl:when test="round(@FileSizeDisplay div (1024 *1024)) &lt; 1"><xsl:value-of select="round(@FileSizeDisplay div 1024)" />kb</xsl:when> 
    					<xsl:otherwise><xsl:value-of select="round(@FileSizeDisplay div (1024 * 1024))" />Mb</xsl:otherwise> 
    				</xsl:choose> 
    			</xsl:if> 
    		</xsl:if>
    	</xsl:variable>
    @FileSizeDisplay is declared and able to be used in this template - if you call it directly it displays the string. EG 4865121

    Thanks for any hints
    Last edited by fidgen; 06-09-2009 at 10:51 AM.

  • #2
    New Coder
    Join Date
    Apr 2005
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Wait.. there was a rogue comma!

    Here's the working code - it includes a better section for calculating the filesizes down to one decimal place. Much neater and more accurate tbh.

    Code:
           <xsl:variable name="FileSizeKb">
    
                 <xsl:if test="string-length(@FileSizeDisplay) &gt; 0"> 
    
                        <xsl:if test="number(@FileSizeDisplay) &gt; 0"> 
    
                               <xsl:choose>
    
                                     <xsl:when test="round(@FileSizeDisplay div 1024) &lt; 1"><xsl:value-of select="@FileSizeDisplay" /> Bytes</xsl:when> 
    
                                     <xsl:when test="round(@FileSizeDisplay div 1048576) &lt; 1"><xsl:value-of select="format-number((@FileSizeDisplay div 1024), '0.0')" />kb</xsl:when> 
    
                                     <xsl:otherwise><xsl:value-of select="format-number((@FileSizeDisplay div 1048576), '0.00')" />Mb</xsl:otherwise> 
    
                               </xsl:choose> 
    
                        </xsl:if> 
    
                 </xsl:if>
    
           </xsl:variable>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •