Click to See Complete Forum and Search --> : Format file size ?


orlio
02-09-2008, 06:28 AM
I've got this line of code:

div.innerHTML = "<span class='someclass'>" + file.size + "</span>";

As default the filesize is shown in bytes. How can I get the filesize to show in B, kB, MB and GB, with that letter after each number? And furthermore, how do I format it so only 3 digits show (like 1.23 or 12.3 or 123)?

JMRKER
02-09-2008, 07:58 AM
See if this works for you. :)

<html>
<head>
<title>File Size Display</title>
<script type="text/javascript">
function DisplayFileSize(bytecount) {
var str = bytecount+' B';
if (Number(bytecount) > 1000) { str = (bytecount/1000).toFixed(2)+' kB'; }
if (Number(bytecount) > 1000000) { str = (bytecount/1000000).toFixed(2)+' MB'; }
if (Number(bytecount) > 1000000000) { str = (bytecount/1000000000).toFixed(2)+' GB'; }
return str;
}
function TestDisplays() {
var fsize = 0;
fsize = 123; document.getElementById('s1').innerHTML = DisplayFileSize(fsize);
fsize = 12345; document.getElementById('s2').innerHTML = DisplayFileSize(fsize);
fsize = 123456; document.getElementById('s3').innerHTML = DisplayFileSize(fsize);
fsize = 1234567; document.getElementById('s4').innerHTML = DisplayFileSize(fsize);
fsize = 123456789; document.getElementById('s5').innerHTML = DisplayFileSize(fsize);
fsize = 1234567890; document.getElementById('s6').innerHTML = DisplayFileSize(fsize);
fsize = 12345678912; document.getElementById('s7').innerHTML = DisplayFileSize(fsize);
}
</script>
</head>
<body onLoad="TestDisplays()">
<div>

<span id="s1"></span><br />
<span id="s2"></span><br />
<span id="s3"></span><br />
<span id="s4"></span><br />
<span id="s5"></span><br />
<span id="s6"></span><br />
<span id="s7"></span><br />

</div>
</body>
</html>

The DisplayFileSize() function could be smaller, but I wanted to show the idea behind the displays.

orlio
02-09-2008, 09:43 AM
Awsome, thank you! :)

JMRKER
02-09-2008, 09:59 AM
You're most welcome.
Glad I was able to help.

felgall
02-09-2008, 03:06 PM
Except that the code there is using the wrong values since the multiplier between the sizes is 1024 and not 1000. It should be:


if (Number(bytecount) > 1024) { str = (bytecount/1024).toFixed(2)+' kB'; }
if (Number(bytecount) > 1048576) { str = (bytecount/1048576).toFixed(2)+' MB'; }
if (Number(bytecount) > 1073741824) { str = (bytecount/1073741824).toFixed(2)+' GB'; }

JMRKER
02-09-2008, 07:39 PM
Oh well. :o That's what happens when you jump before you think!
Self evaluation: Concept: A Implementation: F Average Grade: C

codingisfun
02-10-2008, 01:12 AM
If I have a filesize = 1048577, first IF statement returns "xxx KB" and
2nd IF returns "xxx MB" right?

JMRKER
02-10-2008, 09:37 AM
No, I think it would return only he 2nd result because of the 'str=...' reassignment. The statement would execute creating the 'xxx KB' but then be quickly over-written by second statement. The third statement would not pass the 'if ()' test.

Have not tried this for real, but that was the intent of the script. :)