Program & Design Tips, tricks, tutorials, and tools on programming & web design

10Jun/090

Human-readable file size in JavaScript

"size" is in bytes, the rest you should be able to figure out.

function readableFileSize(size) {
    var units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
    var i = 0;
    while(size >= 1024) {
        size /= 1024;
        ++i;
    }
    return size.toFixed(1) + ' ' + units[i];
}
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.