Javascript Number toBytes()

Description

Javascript Number toBytes()


/* eslint-disable no-extend-native */
Number.prototype.toBytes = function () {
  if (this === 0) { return '0 bytes' }
  var i = parseInt(Math.floor(Math.log(this) / Math.log(1024)))
  var r = Math.round(this / Math.pow(1024, i) * 10) / 10
  return [r, ['bytes', 'KB', 'MB', 'GB', 'TB'][i]].join(' ')
}



PreviousNext

Related