Created
March 19, 2013 03:05
-
Star
(105)
You must be signed in to star a gist -
Fork
(26)
You must be signed in to fork a gist
-
-
Save lanqy/5193417 to your computer and use it in GitHub Desktop.
JavaScript To Convert Bytes To MB, KB, Etc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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]; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Shouldn't
999999999return1 GBand not1000 MB, since1000000000returns1 GB.