Skip to content

Instantly share code, notes, and snippets.

@gordio
Forked from 140bytes/LICENSE.txt
Created December 10, 2012 22:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gordio/4253976 to your computer and use it in GitHub Desktop.
Save gordio/4253976 to your computer and use it in GitHub Desktop.
Bytes Size to Human String

Bytes Size to Human String

Convert File Size Int to human readable format.

Usage

var size2str = function(s,d,b){var i=0,t=" KMGTPEZY";while(s>999){s/=(b?1000:1024);i++} return s.toFixed(d)+" "+(i?t[i]:"")+(b||i==0?"B":"iB")};
size2str(1025, 2)
# function(bytes_size, digits_after_dot, true_for_MB_standart)
> "1.00 KiB"

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.

z = function(
s, // size in butes
d, // digits after dot
b // true for MB standart
) {
var i = 0, // prefix id
t = " KMGTPEZY"; // prefix's "array"
while (s > 999) {
s /= (b ? 1000 : 1024); // choice standart
i++; // next prefix
}
return s.toFixed(d) + // size as "float"
" " + // add space
(i ? t[i] : "") + // don't add space if 0
(b || i == 0 ? "B" : "iB"); // choice standart + hack for iB
};
function(s,d,b){var i=0,t=" KMGTPEZY";while(s>999){s/=(b?1000:1024);i++} return s.toFixed(d)+" "+(i?t[i]:"")+(b||i==0?"B":"iB")};
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "size_to_str",
"description": "Convert size in bytes to human readable format.",
"keywords": [
"string",
"number"
]
}
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Size in bytes to human reahable string</title>
<style type="text/css">
h4 {margin-bottom: 0}
pre {
margin: 1px 0;
padding: 4px;
border: 1px solid #888;
}
</style>
</head>
<body>
<div>
Default: 823964<br>
sz2hstr: <span id="testelement">823964</span>
</div>
<h4>Usage:</h4>
<pre><code>var sz2hs = function(s,d,b){var i=0,t=" KMGTPEZY";while(s>999){s/=(b?1000:1024);i++} return s.toFixed(d)+" "+(i?t[i]:"")+(b||i==0?"B":"iB")};
human_readable_string = sz2hs(foo.bytes, 2); </code></pre>
<script type="text/javascript">
var sz2hs = function(s,d,b){var i=0,t=" KMGTPEZY";while(s>999){s/=(b?1000:1024);i++} return s.toFixed(d)+" "+(i?t[i]:"")+(b||i==0?"B":"iB")};
e = document.getElementById('testelement')
e.textContent = sz2hs(e.textContent, 2);
</script>
</body>
</html>
@atk
Copy link

atk commented Dec 11, 2012

The "for" loop is shorter: t[undefined] is falsy, so t[i]||"" works, too (124bytes):

function(s,d,b){for(var i=0,t=" KMGTPEZY";s>999;s/=(b?1000:1024),i++);return s.toFixed(d)+" "+(t[i]||"")+(b||i==0?"B":"iB")}

Nice touch to give the user the choice between mega- and mebibytes.

@xpansive
Copy link

I got it down to 110 by reorganizing a bit, changing s/=(b?1000:1024) to the shorter s/=1e3+!b*24 (you don't need brackets even with your method), and changing b||i==0?"B":"iB" to b|!i?"B":"iB".

function(s,d,b,i){for(i=0;s>999;s/=1e3+!b*24)i++;return s.toFixed(d)+" "+(" KMGTPEZY"[i]||"")+(b|!i?"B":"iB")}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment