Last active
August 28, 2018 19:59
-
-
Save dansmith01/9991d1334a31fd777f02e7af09781904 to your computer and use it in GitHub Desktop.
Convert bytes to human readable, keeping 3 significant figures.
This file contains 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
function bytesSI(bytes) { | |
if (isNaN(parseFloat(bytes)) || !isFinite(bytes)) return bytes; | |
var suffix = ' Bytes'; | |
var suffixes = [' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB']; | |
while (suffixes.length && bytes >= 1000) { | |
bytes = (bytes + 0.0001) / 1000; | |
suffix = suffixes.shift(); | |
} | |
return bytes.toString().substr(0,4).concat(suffix).replace(". ", " "); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment