# Dealing with KB, MB, GB and TB and bytes

This function is simple, it gets a number of bytes and returns its representation in either Bytes, KB, MB, GB and TB.

As simple as it is, I am sure there are other (and perhaps better ways) to write it.

function kmgtbytes (num) {
if (num > 0 ){
if (num < 1024)             { return [num, "Bytes"] }
if (num < 1048576)          { return [num/1024, "KB"] }
if (num < 1073741824)       { return [num/1024/1024, "MB"] }
if (num < 1099511600000)    { return [num/1024/1024/1024, "GB"] }

return [num/1024/1024/1024/1024, "TB"]
}

return num
}


## migrated from stackoverflow.comAug 16 '13 at 15:42

This question came from our site for professional and enthusiast programmers.

• jsPerf for all current answers – Ry- Aug 16 '13 at 17:29
• Having your function return a different type as the default will probably cause type errors everywhere – Eric Aug 16 '13 at 18:00
• This function lacks only a better name and an introductory comment (incl. examples of possible outputs). I haven't managed to come up with either, by the way. – sbichenko Aug 16 '13 at 21:02
• You may find reading how others have done it in the past enlightening. – kojiro Aug 17 '13 at 12:10

Here's a slightly less repetitive way to write it since we know each unit is 1024 as large as the last:

function kmgtbytes (num) {
var unit, units = ["TB", "GB", "MB", "KB", "Bytes"];
for (unit = units.pop(); units.length && num >= 1024; unit = units.pop()) {
num /= 1024;
}
return [num, unit];
}

• I'll admit that using pop() is a little inefficient as you're creating and mutating the array of units on every call. Whether this is actually a problem depends on how often you expect to be calling this function, and your definition of "better" as used in the question :) – Nick Aug 18 '13 at 12:45
• Why is the result of this function an array? Very surprising. – Tom Pažourek Aug 19 '14 at 13:46

var units=["Byte","KB","MB","GB","TB","PB"];

function getUnit(bytes){
for(var i in units){
if(bytes<1024) return bytes+" "+units[i];
bytes/=1024;
}
}


Edit:

It prevents you from precalculating your values for each if-clause. It's easily extensible: Just add another unit, wherever you want. And for the rest: it does its job in an easy understandable way. No rocket science.

• Please don’t use for…in loops to iterate arrays. It’s darn slow and not necessarily accurate. – Ry- Aug 17 '13 at 5:19
• Optimizing this loop is barking up the wrong tree, even if it were slow. If that's the only "bottleneck" in your code, everything is okay I would say. – Thomas Junk Aug 17 '13 at 17:02
• Yes, but a for...in loop isn't guaranteed to execute in order. More reading here. – jahroy Aug 23 '13 at 6:11

You can use logarithms:

var sizes = ["bytes", "KB", "MB", "GB", "TB"];

function formatSize(bytes) {
var l = Math.min(sizes.length - 1, Math.log(bytes) / Math.LN2 / 10 | 0);
return [bytes / Math.pow(1024, l), sizes[l]];
}


The confusing part, Math.log(bytes) / Math.LN2 / 10 | 0, gets the base-1024 logarithm of bytes and truncates it.

• The use of logarithm is a wise one! – Tzury Bar Yochay Aug 16 '13 at 15:57
• Excessive use of floating point is icky for my taste, and also likely to perform worse. – 200_success Aug 16 '13 at 17:17
• @200_success: You can’t avoid floating-point here. 1024^4 > 2^32. Apart from that, “once, which forms the crux of this shortcut” is difficult to avoid :P – Ry- Aug 16 '13 at 17:18
• No doubt, the problem itself requires floating point, as the return value is FP. It's the Math.log() that I find excessive. – 200_success Aug 16 '13 at 17:22
• @200_success: This is the fastest answer so far on Firefox 23.0, for what it’s worth. (Which is not very much.) – Ry- Aug 16 '13 at 17:35

From the Readability/Cleanliness perspective I'd argue that your code is the easiest to read vs any answer you've received thus far. I knew in 3 seconds the purpose of all of that code and wouldn't need any comments to further explain it.

From the robustness perspective it all boils down to the use case of the code. If you know without a doubt that you will only ever see positive sized numbers <1024 TB I'd say again your code is fine. If you wanted to handle file size differences (as in 100kb-150kb = -50kb) or file sizes beyond 1023TB then clearly you need to enhance your approach.

As a side note it would appear size=0 would return [0,] but size of 1 would return [1,Bytes]. You may want to return [0,Bytes]

var units = ["Bytes", "KB", "MB", "GB", "TB"];

function unitize(num) {
return unitizer(num, 0);
}

function unitizer(num, level) {
if (num < 1024 || level > units.length - 1) {
return num + " " + units[level];
} else {
return unitizer(num / 1024, level + 1);
}
}


The readability of your original function is tough to beat. At least for now, it's also the most performant of the bunch.

I'd use a while loop, divide by 1024 and keep track of how many times you divided.

This is my completely bonkers function:

function resolve_to_power_of(bytes, power) {
var powers;
powers = 0;
while (bytes >= power) {
powers += 1;
bytes = bytes / power;
}
return {
quantity: bytes,
powers: powers
};
}

function format_default(bytes) {
var resolved, descriptors;
resolved = resolve_to_power_of(bytes, 1024);
descriptors = [ "B", "KB", "MB", "TB", "GB", "PB", "EB", "ZB", "YB" ];
return Math.ceil(resolved.quantity) + " " + descriptors[resolved.powers];
}

format_default(1024); // 1 KB
format_default(1000); // 1000 B
format_default(102400); // 100 KB


The reason it's set up like this is because it also supports dividing by 1000 and other byte lables like [ "B", "K", "M" .. ] etc. following IEC and SI standards.

• why while loop? – Tzury Bar Yochay Aug 16 '13 at 15:56
• Because it makes the most sense: keep dividing while the number is larger than or equal to 1024. – Frits van Campen Aug 16 '13 at 15:59
• You can use ++ and /= and var powers = 0;, if you’re not against that sort of formatting. (I’d do /=, though.) – Ry- Aug 16 '13 at 16:03
• @minitech, it's good practice to separate variable declaration and instantiation in JavaScript. Scripts should generally be written as with an order of: variable declaration, function declaration, initialization, execution. Doing this the more verbose way helps avoid some variable hoisting issues, and some initialization ordering issues. – zzzzBov Aug 16 '13 at 17:26
• @minitech My coding style aims to increase readability, extensibility and maintainability and decrease the chance of errors. This code passes JSLint checks. Nick's solution is very clever, it uses only 6 lines. I also had to read it twice to make sure there is no off-by-one error. It has the most upvotes because we measure code quality by the least number of characters and best use of obscure functions. /sarcasm. If I found this code in one of my applications I would ask him to rewrite it. – Frits van Campen Aug 16 '13 at 19:06

You can loop through the thousands until the number is small enough, then just return the remaining number and the corresponding unit:

function kmgtbytes(num) {
for (var i = 0; num >= 1024 && i < 4; i++) num /= 1024;
return [num, ["Bytes","kB","MB","GB","TB"][i]];
}