Deprecated: Array and string offset access syntax with curly braces is deprecated in /customers/1/2/4/mrwaggel.be/httpd.www/admin/kernel/helpers/text.class.php on line 171 Notice: Trying to access array offset on value of type null in /customers/1/2/4/mrwaggel.be/httpd.www/admin/kernel/db/db_posts.class.php on line 390 Notice: Trying to access array offset on value of type null in /customers/1/2/4/mrwaggel.be/httpd.www/admin/kernel/db/db_pages.class.php on line 337 Notice: Trying to access array offset on value of type null in /customers/1/2/4/mrwaggel.be/httpd.www/admin/kernel/db/db_comments.class.php on line 283 Deprecated: Function get_magic_quotes_runtime() is deprecated in /customers/1/2/4/mrwaggel.be/httpd.www/admin/boot/rules/4-remove_magic.bit on line 18 Deprecated: Function get_magic_quotes_gpc() is deprecated in /customers/1/2/4/mrwaggel.be/httpd.www/admin/boot/rules/4-remove_magic.bit on line 23 - functionMr.Waggel's blog

Javascript human readable byte size function

31 March, 2020

Here is a small function which converts a bytes in to a readable format (B, KB, MB, GB, TB).

Function

const TB = 1000000000000;
const GB = 1000000000;
const MB = 1000000;
const KB = 1000;

function bytesReadable(byteSize, decimals) {
    let unit;
    let i;
    let remainder;

    // Get the whole number, and the remainder for the
    // decimals later on.
    if (byteSize > TB) {
        unit = "TB";
        i = Math.floor(byteSize / TB);
        remainder = byteSize - (i * TB);
    }else if (byteSize > GB) {
        unit = "GB";
        i = Math.floor(byteSize / GB);
        remainder = byteSize - (i * GB);
    }else if (byteSize > MB) {
        unit = "MB";
        i = Math.floor(byteSize / MB);
        remainder = byteSize - (i * MB);

    }else if (byteSize > KB){
        unit = "KB";
        i = Math.floor(byteSize / KB),
            remainder = byteSize - i * KB;
    }else{
        // Can't go smaller than bytes!
        return i.toString() + " " + unit;
    }

    if (decimals === 0){
        return i.toString() + " " + unit;
    }

    // Now we calculate the eventual missing leading
    // zeroes to pad up if needed.
    let width;
    if (remainder > GB){
        width = 12;
    }else if (remainder > MB){
        width = 9;
    }else if (remainder > KB){
        width = 6;
    }else{
        width = 3;
    }

    // Fill up leading zeroes if they are missing
    let remainderString = remainder.toString();

    for (let i = remainderString.length; i < width; i++){
        remainderString = "0" + remainderString;
    }

    // Check to prevent unwanted behaviour
    if (decimals > remainderString.length){
        decimals = remainderString.length;
    }

    return i.toString() + "." + remainderString.substr(0, decimals) + " " + unit;
}

Sample

console.log(bytesReadable(425005, 2));
console.log(bytesReadable(8741208, 2));
console.log(bytesReadable(114448910, 2));
console.log(bytesReadable(557891, 2));
console.log(bytesReadable(114710, 2));
console.log(bytesReadable(8933578, 2));
console.log(bytesReadable(5849684981, 2));
console.log(bytesReadable(12033687, 2));
console.log(bytesReadable(742289, 2));
console.log(bytesReadable(678007439, 2));

// Output
425.00 KB
  8.74 MB
114.44 MB
557.89 KB
114.71 KB
  8.93 MB
  5.84 GB
 12.03 MB
742.28 KB
678.00 MB

 

 

Golang reflect if initialized struct has member method or fields

12 April, 2018

In Go it is possible to check if any pointer to an initialized struct has a given method or field. The use of this is to make sure that you can call a given method, or access a given field without receiving a runtime panic.

Home