Human readable file size

by Arjan Haverkamp

JavaScript

function humanFileSize(size) {
	var i = -1;
	var byteUnits = ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
	if (size < 1024) { return size + ' Bytes'; }
	do {
		size = size / 1024;
		i++;
	} while (size > 1024);
	return Math.max(size, 0.1).toFixed(1) + byteUnits[i];
};
   
alert(humanFileSize(1024))