Skip to content

Instantly share code, notes, and snippets.

@blurymind
Forked from lanqy/bytesToSize.js
Created September 10, 2024 22:09
Show Gist options
  • Save blurymind/98e51fa0f010dbaf3e5ff024377a74b1 to your computer and use it in GitHub Desktop.
Save blurymind/98e51fa0f010dbaf3e5ff024377a74b1 to your computer and use it in GitHub Desktop.

Revisions

  1. blurymind revised this gist Sep 10, 2024. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion bytesToSize.js
    Original file line number Diff line number Diff line change
    @@ -5,4 +5,10 @@ function bytesToSize(bytes) {
    var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
    if (i == 0) return bytes + ' ' + sizes[i];
    return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];
    };
    };

    const getBase64StringSizeInbites = string => {
    string = string.substring(string.indexOf(',') + 1, string.length)
    const bites = ((string.length * 6) / 8); // / 1000 is kb
    return bites
    }
  2. @lanqy lanqy created this gist Mar 19, 2013.
    8 changes: 8 additions & 0 deletions bytesToSize.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    // from http://scratch99.com/web-development/javascript/convert-bytes-to-mb-kb/
    function bytesToSize(bytes) {
    var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
    if (bytes == 0) return 'n/a';
    var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
    if (i == 0) return bytes + ' ' + sizes[i];
    return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];
    };