Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
number to human byte size
function(
n, // number (in bytes)
p, // precision (default: 0)
b, // base (1000 or 1024, default: 1000)
i, // internal counter
){
for(
b=b||1e3; // init base with default value 1e3 == 1000
i=-~i, // nasty trick to initialize and increase counter, see comments
n>=b // comparator (number >= base)
&& i<9 // and sanity check (change this, if you update the unit prefixes)
;
)
n/=b; // divide number by base
return
--i // if number >= base (didn't increase counter)
? // either
n.toFixed(p) // format number with precision
+"\xA0" // add non-breaking space
+" KMGTPEZY"[i] // get unit prefix (kilo, mega, giga, ...)
+(b&8?'B':'iB') // if the base is 1024 add the "i" character to unit prefix
: // or
n+"\xA0Byte" // just return byte + non-breaking space + "Byte" unit
}
function(n,p,b,i){for(b=b||1e3;i=-~i,i<9&&n>=b;)n/=b;return--i?n.toFixed(p)+"\xA0"+" KMGTPEZY"[i]+(b&8?'B':'iB'):n+"\xA0Byte"}
// older IE compatible version, see comments
function(n,p,b,i){for(b=b||1e3;i=-~i,i<9&&n>=b;)n/=b;return--i?n.toFixed(p)+"\xA0"+" KMGTPEZY".charAt(i)+(b&8?'B':'iB'):n+"\xA0Byte"}
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": "NumberToHumanBytes",
"description": "Formats a number into a human understandable representation of Bytes",
"keywords": [
"Bytes",
"Number",
"format"
]
}
<script>
b=function(n,p,b,i){for(b=b||1e3;i=-~i,i<9&&n>=b;)n/=b;return--i?n.toFixed(p)+"\xA0"+" KMGTPEZY"[i]+(b&8?'B':'iB'):n+"\xA0Byte"}
console.log( b(1), "==", "1 Byte" )
console.log( b(22140), "==", "22 KB" )
console.log( b(22140, 2), "==", "21.14 KB" )
console.log( b(22140, 2, 1024), "==", "21.62 KiB" )
console.log( b(22140, 5, 1024), "==", "21.62109 KiB" )
console.log( b(2214023452335, 3),"==", "2.214 TB" )
console.log( b(221402345233435345345345345345334534534545), "==", "221402345233435360 YB" )
</script>
@maettig

This comment has been minimized.

Copy link

maettig commented Apr 3, 2012

Save a few bytes by replacing while with for and b==1024 with b&8 and rearranging the code a little bit.

function(n,b,p){b=b||1000;for(var i=0,_="&nbsp;";n>=b;i++)n/=b;return i--?n.toFixed(p)+_+"KMGTPEZY"[i]+(b&8?'B':'iB'):n+_+"Byte"}

Also, your index.js should in a single line to get the correct byte count.

@atk

This comment has been minimized.

Copy link

atk commented Apr 3, 2012

"\xA0" is shorter than " ", and you can lose the var statement and the "_":

function(n,b,p,i){for(b=b||1000,i=0;n>=b;i++)n/=b;return i--?n.toFixed(p)+"\xA0"+"KMGTPEZY"[i]+(b&8?'B':'iB'):n+"\xA0Byte"}

Also, "KMGTPEZY"[i] will not work in older IE versions (you should at least mention this). Fixed for IE:

function(n,b,p,i){for(b=b||1000,i=0;n>=b;i++)n/=b;return i--?n.toFixed(p)+"\xA0"+"KMGTPEZY".charAt(i)+(b&8?'B':'iB'):n+"\xA0Byte"}

@xpansive

This comment has been minimized.

Copy link

xpansive commented Apr 3, 2012

1000 can be replaced with 1e3, saving a byte:
function(n,b,p,i){for(b=b||1e3,i=0;n>=b;i++)n/=b;return i--?n.toFixed(p)+"\xA0"+"KMGTPEZY"[i]+(b&8?'B':'iB'):n+"\xA0Byte"}

@atk

This comment has been minimized.

Copy link

atk commented Apr 4, 2012

Nasty trick, saving another byte by not initializing i (tsaniel had tried this, but forgot to insert a character into the prefix table):

function(n,b,p,i){for(b=b||1e3;i=-~i,n>=b;)n/=b;return--i?n.toFixed(p)+"\xA0"+" KMGTPEZY"[i]+(b&8?'B':'iB'):n+"\xA0Byte"}

@max-power

This comment has been minimized.

Copy link
Owner Author

max-power commented Apr 4, 2012

sweet! we're down to 121 bytes. gist is updated!

@atk

This comment has been minimized.

Copy link

atk commented Apr 4, 2012

Your proposed sanity check will break things, becuase it overwrites the original test of n>=b; you may want to use && instead of ,

function(n,b,p,i){for(b=b||1e3;i=-~i,i<9&&n>=b;)n/=b;return--i?n.toFixed(p)+"\xA0"+" KMGTPEZY"[i]+(b&8?'B':'iB'):n+"\xA0Byte"}

@max-power

This comment has been minimized.

Copy link
Owner Author

max-power commented Apr 4, 2012

i just saw the tests failing. thanks a lot. this works now. example added.
now back to real work...

@sfinktah

This comment has been minimized.

Copy link

sfinktah commented Nov 9, 2016

Okay, so this isn't actually shorter, just faster. Won't work for your extremely large YB number due to 32-bit limitations on Math.log.

function(n,p,b,i,d){return(d=Math.log,b=b||1E3,i=d(n)/d(b)|0,n/Math.pow(b,i)).toFixed(p)+"\xa0"+(i?" kMGTPEZY"[i]+(b&8?"B":"iB"):"bytes")};

n.b., the correct prefix for kilo is k

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.