Last active
October 14, 2015 16:18
-
-
Save rodrigopolo/1f159dfd7b908e1c369a to your computer and use it in GitHub Desktop.
Human readable bandwith
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 bandWidth(bits) { | |
var unit = 1000; | |
if (bits < unit) return (bits % 1 === 0 ? bits : bits.toFixed(2)) + "B"; | |
var exp = parseInt(Math.log(bits) / Math.log(unit)); | |
var pre = "KMGTPE"[exp-1] + 'bps'; | |
var n = bits / Math.pow(unit, exp); | |
return (n % 1 === 0 ? n : n.toFixed(2))+pre; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment