Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I got this code to covert size in bytes via PHP.

Now I want to convert those sizes to human readable sizes using JavaScript. I tried to convert this code to JavaScript, which looks like this:

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 bytes"; }   return bytes; } 

Is this the correct way of doing this? Is there an easier way?

like image 882
l2aelba Avatar asked Apr 09 '13 11:04

l2aelba


People also ask

How do you convert Byte size?

You can convert 512 byte blocks to bytes by multiplying them by 512. For example, six 512-byte-blocks multiplied by 512 equals 3,072 bytes.

How do you convert bytes to GB manually?

1 KiB = 1024 Bytes Note the slight difference between gigabyte and gibibyte: 1 GB = 0.931 GiB .


1 Answers

From this: (source)

function bytesToSize(bytes) {    var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];    if (bytes == 0) return '0 Byte';    var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));    return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i]; } 

Note: This is original code, Please use fixed version below.


Fixed version, unminified and ES6'ed: (by community)

function formatBytes(bytes, decimals = 2) {     if (bytes === 0) 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]; } 

Fixed Version (by StackOverflow's community, minified by JSCompress)

function formatBytes(a,b=2,k=1024){with(Math){let d=floor(log(a)/log(k));return 0==a?"0 Bytes":parseFloat((a/pow(k,d)).toFixed(max(0,b)))+" "+["Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"][d]}} 

Usage :

// formatBytes(bytes,decimals)  formatBytes(1024);       // 1 KB formatBytes('1024');     // 1 KB formatBytes(1234);       // 1.21 KB formatBytes(1234, 3);    // 1.205 KB 

Demo / source :

function formatBytes(bytes, decimals = 2) {     if (bytes === 0) 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 ** var p = document.querySelector('p'),     input = document.querySelector('input');      function setText(v){     p.innerHTML = formatBytes(v); } // bind 'input' event input.addEventListener('input', function(){      setText( this.value ) }) // set initial text setText(input.value);
<input type="text" value="1000"> <p></p>

PS : Change k = 1000 or sizes = ["..."] as you want (bits or bytes)

like image 151
Aliceljm Avatar answered Sep 23 '22 08:09

Aliceljm