Bytes To Size - Node.js Number

Node.js examples for Number:Format

Description

Bytes To Size

Demo Code


function bytesToSize(bytes) {
    if (bytes === undefined || bytes === 0) { return '0' }
    var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']
    var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10)
    var decimals = Math.max(0, i-1)
    return (bytes / Math.pow(1024, i)).toFixed(decimals) + ' ' + sizes[i]
}

Related Tutorials