<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>My Distributed Life &#187; JavaScript</title>
	<atom:link href="http://mydistributedlife.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://mydistributedlife.com</link>
	<description>Musings of a Geek/Husband/Father</description>
	<lastBuildDate>Thu, 30 Apr 2015 05:11:59 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.1.1</generator>
	<item>
		<title>File Size to String in JavaScript</title>
		<link>http://mydistributedlife.com/2012/03/file-size-to-string-in-javascript/</link>
		<comments>http://mydistributedlife.com/2012/03/file-size-to-string-in-javascript/#comments</comments>
		<pubDate>Fri, 23 Mar 2012 05:14:35 +0000</pubDate>
		<dc:creator><![CDATA[Garett]]></dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://mydistributedlife.com/?p=21</guid>
		<description><![CDATA[Recently, I needed to figure out a way to convert the size of a file, which is being returned in bytes, to a string representation.  So, given a file size in bytes like 349792 the string “342KB&#8221; would be returned.  Inspired by the following StackOverflow post, I came up with this JavaScript version. function fileSizeToString(size) ... <span class="more"><a class="more-link" href="http://mydistributedlife.com/2012/03/file-size-to-string-in-javascript/">[Read more...]</a></span>]]></description>
				<content:encoded><![CDATA[<p>Recently, I needed to figure out a way to convert the size of a file, which is being returned in bytes, to a string representation.  So, given a file size in bytes like 349792 the string “342KB&#8221; would be returned.  Inspired by the following <a href="http://stackoverflow.com/questions/281640/how-do-i-get-a-human-readable-file-size-using-net" target="_blank">StackOverflow post</a>, I came up with this JavaScript version.</p>
<pre class="brush: javascript auto-links: false;">function fileSizeToString(size) {
    var suffix = ["B", "KB", "MB", "GB"];

    var place = Math.floor(Math.log(size) / Math.log(1024));
    var fileSize = Math.round(size / Math.pow(1024, place));

    return (fileSize + suffix[place]);
};</pre>
<p>As you can see this is a relatively simple function, and really boils down to two lines of code. The first line</p>
<pre class="brush: javascript auto-links: false;">var place = Math.floor(Math.log(size) / Math.log(1024));</pre>
<p>determines which suffix will be used. While the second line</p>
<pre class="brush: javascript auto-links: false;">var fileSize = Math.round(size / Math.pow(1024, place));</pre>
<p>calculates the file size, which is typically <a href="http://en.wikipedia.org/wiki/File_size" target="_blank">measured</a> in units of 1024.</p>
<p>In my case I only needed to support up to gigabyte file sizes, but the function could easily be modified to support larger sizes.</p>
<p>Happy Coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://mydistributedlife.com/2012/03/file-size-to-string-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
