Correct Way to Convert Size in Bytes to Kb, Mb, Gb in JavaScript

Correct way to convert size in bytes to KB, MB, GB in JavaScript

From this: (source)


Unminified and ES6'ed: (by the community)

function formatBytes(bytes, decimals = 2) {
if (!+bytes) return '0 Bytes'

const k = 1024
const dm = decimals < 0 ? 0 : decimals
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']

const i = Math.floor(Math.log(bytes) / Math.log(k))

return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`
}

// Demo code
document.body.innerHTML += `<input type="text" oninput="document.querySelector('p').innerHTML=formatBytes(this.value)" value="1000"><p>1000 Bytes</p>`

Convert Bytes to KB/MB in Javascript

you need to divide the totalSize through 1024^2 for MB, for KB you need 1024^1, and for GB you should divide throug 1024^4

var totalSizeKB = totalsize / Math.pow(1024,1)
var totalSizeMB = totalsize / Math.pow(1024,2)
var totalSizeGB = totalsize / Math.pow(1024,3)

which wil give you 3.000241279602051MB

how to convert kilobytes to megabytes in javascript

Your function is correct. It accepts bytes only. But what your trying to do is formatSizeUnits(4000). this is wrong and the expected output is 3.91 MB as it is divided by 1024 and not with 1000. The correct ways is to call like

 formatSizeUnits(4000*1024)  // beacuse 4000 is in KB and convert into bytes

See the below snippet to get the correct answer

function formatSizeUnits(bytes){      if      (bytes>=1073741824) {bytes=(bytes/1073741824).toFixed(2)+' GB';}      else if (bytes>=1048576)    {bytes=(bytes/1048576).toFixed(2)+' MB';}      else if (bytes>=1024)       {bytes=(bytes/1024).toFixed(2)+' KB';}      else if (bytes>1)           {bytes=bytes+' bytes';}      else if (bytes==1)          {bytes=bytes+' byte';}      else                        {bytes='0 byte';}      return bytes;}

document.write(formatSizeUnits(4000*1024));

Correct way to convert size in bytes to KB, MB, GB in JavaScript

From this: (source)


Unminified and ES6'ed: (by the community)

function formatBytes(bytes, decimals = 2) {
if (!+bytes) return '0 Bytes'

const k = 1024
const dm = decimals < 0 ? 0 : decimals
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']

const i = Math.floor(Math.log(bytes) / Math.log(k))

return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`
}

// Demo code
document.body.innerHTML += `<input type="text" oninput="document.querySelector('p').innerHTML=formatBytes(this.value)" value="1000"><p>1000 Bytes</p>`

Is 1KB = 1.024 bytes OR 1.000 bytes?

Is 1KB = 1.024 bytes OR 1.000 ?

Yes, in causal discourse, it is either one - context sensitive.

  • Memory size tends to use 1024.

  • File size tends to use 1000.

Exceptions are common.

Else see Kilobyte.

Pedantic concerns like this include using K, when k should be used as in 1kB.

Format bytes to kilobytes, megabytes, gigabytes

function formatBytes($bytes, $precision = 2) { 
$units = array('B', 'KB', 'MB', 'GB', 'TB');

$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);

// Uncomment one of the following alternatives
// $bytes /= pow(1024, $pow);
// $bytes /= (1 << (10 * $pow));

return round($bytes, $precision) . ' ' . $units[$pow];
}

(Taken from php.net, there are many other examples there, but I like this one best :-)



Related Topics



Leave a reply



Submit