Javscript ES5 or earlier:
function bytesToMegaBytes(bytes) { return bytes / (1024*1024); }
Javscript ES6 (arrow functions):
const bytesToMegaBytes = bytes => bytes / (1024*1024);
If you want to round to digits after the decimal point, then:
function (bytes, roundTo) { var converted = bytes / (1024*1024); return roundTo ? converted.toFixed(roundTo) : converted; }
In E6 or beyond:
const bytesToMegaBytes = (bytes, digits) => roundTo ? (bytes / (1024*1024)).toFixed(digits) : (bytes / (1024*1024));
- Details on Number.prototype.toFixed () .
- Below you can view the file size conversion chart for further assistance. Conversion table
source share