Skip to content

Instantly share code, notes, and snippets.

Created April 28, 2015 07:02
Show Gist options
  • Save anonymous/1da835878b458ee2c636 to your computer and use it in GitHub Desktop.
Save anonymous/1da835878b458ee2c636 to your computer and use it in GitHub Desktop.
Convert number to Bytes size
Object.defineProperty(Number.prototype,'fileSize',{value:function(a,b,c,d){
if(this == 0){
return "0.0 B";
}else if(this < 0.001){
return this.toExponential(1) + " B";
}else if(this < 1){
return this.toLocaleString() + " B";
}
return (a=a?[1e3,'k','B']:[1024,'K','iB'],b=Math,c=b.log,
d=c(this)/c(a[0])|0,this/b.pow(a[0],d)).toFixed(1)
+' '+(d?(a[1]+'MGTPEZY')[--d]+a[2]:'B');
},writable:false,enumerable:false});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment