-
-
Save max-power/2288464 to your computer and use it in GitHub Desktop.
number to human byte size
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "NumberToHumanBytes", | |
"description": "Formats a number into a human understandable representation of Bytes", | |
"keywords": [ | |
"Bytes", | |
"Number", | |
"format" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
n.b., the correct prefix for kilo is k