Skip to content

Instantly share code, notes, and snippets.

@AlttiRi
Last active October 19, 2023 04:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlttiRi/ee82de3728624f997b38e4fb90906914 to your computer and use it in GitHub Desktop.
Save AlttiRi/ee82de3728624f997b38e4fb90906914 to your computer and use it in GitHub Desktop.
How to format file size bytes into the human readable form like it Windows Explorer does.

How to format file size bytes into the human readable form like it Windows Explorer does

...like it Windows Explorer does.

JavaScript code:

/**
 * Formats bytes mostly like Windows does,
 * but in some rare cases the result is different.
 * Check the file with tests.
 * @see https://github.com/AlttiRi/directory-snapshot-explorer/blob/master/tests/win-like-file-sizes.test.js
 * @param {Number} bytes
 * @return {string}
 */
function bytesToSizeWinLike(bytes) {
    if (bytes < 1024) { return bytes + " B"; }
    const sizes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
    let i = Math.floor(Math.log(bytes) / Math.log(1024));
    let result = bytes / Math.pow(1024, i);
    if (result >= 1000) {
        i++;
        result /= 1024;
    }
    return toTruncPrecision3(result) + " " + sizes[i];
}

/**
 * @see https://github.com/AlttiRi/directory-snapshot-explorer/blob/master/tests/trunc-with-precision-3.test.js
 * @param {Number} number
 * @return {string}
 */
function toTruncPrecision3(number) {
    let result;
    if (number < 10) {
        result = Math.trunc(number * 100) / 100;
    } else if (number < 100) {
        result = Math.trunc(number * 10) / 10;
    } else if (number < 1000) {
        result = Math.trunc(number);
    }
    if (number < 0.1) {
        return result.toPrecision(1);
    } else if (number < 1) {
        return result.toPrecision(2);
    }
    return result.toPrecision(3);
}

See the test files to look at the input data and the corresponding output data:

The function's result is different from the Windows Explorer's result in some rare cases — see the test file.

But anyway the result of this function is the closest to the value that Windows Explorer shows, all existing functions on stackoverflow return a less closest result.


Taken from here: https://github.com/AlttiRi/directory-snapshot-explorer

@AlttiRi
Copy link
Author

AlttiRi commented Nov 29, 2021

// https://gist.github.com/AlttiRi/ee82de3728624f997b38e4fb90906914
function bytesToSizeWinLike(t){if(t<1024)return t+" B";let n=Math.floor(Math.log(t)/Math.log(1024)),o=t/Math.pow(1024,n);return o>=1e3&&(n++,o/=1024),toTruncPrecision3(o)+" "+["B","KB","MB","GB","TB","PB","EB","ZB","YB"][n]}
function toTruncPrecision3(t){let n;return t<10?n=Math.trunc(100*t)/100:t<100?n=Math.trunc(10*t)/10:t<1e3&&(n=Math.trunc(t)),t<.1?n.toPrecision(1):t<1?n.toPrecision(2):n.toPrecision(3)}

/* Minified version */

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment