Q . How to convert bytes size to MB size?

Dev Singh. asked, Aug 09 ' 2017

I google and found the below code.

function formatBytes(bytes,decimals) {
   if(bytes == 0) return '0 Bytes';
   var k = 1000,
       dm = decimals || 2,
       sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
       i = Math.floor(Math.log(bytes) / Math.log(k));
   return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}

But I didn't get how to use this.

  • JavaScript
  • bytes_to_mb
  • convert_size

1 Answers

var sizeInMB = (sizeInBytes / (1024*1024)).toFixed(2);
alert(sizeInMB + 'MB');

 

Leave a Ansewer