Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Number.formatBytes #10

Open
jnbdz opened this issue Nov 9, 2015 · 0 comments
Open

Number.formatBytes #10

jnbdz opened this issue Nov 9, 2015 · 0 comments
Labels

Comments

@jnbdz
Copy link
Member

jnbdz commented Nov 9, 2015

/*

---
description: Utility method to format bytes into the most logical magnitude (KB, MB, or GB).

authors:
  - Jean-Nicolas Boulay (http://jean-nicolas.com/)

license:
  - MIT-style license

requires:
 - core/1.4

provides:
  - Number.formatBytes
...
*/

Number.implement({
    formatBytes: function(showUnit, toFixed) {
        var units = (typeOf(showUnit) == 'array' && showUnit.length == 9)?showUnit:['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
            showUnit = (typeOf(showUnit) == 'null')?true:false,
            toFixed = (typeOf(toFixed) == 'null')?2:toFixed,
            bytes = this,
            i;

        for (i = 0; bytes >= 1024 && i < 8; i++) {
            bytes /= 1024;
        }

        var unit = (showUnit)?units[i]:'';
        return bytes.toFixed(toFixed) + unit;
    }
});
@jnbdz jnbdz added the Proposal label Nov 9, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant