Skip to content

Instantly share code, notes, and snippets.

@rodrigopolo
Last active October 14, 2015 16:18
Show Gist options
  • Save rodrigopolo/1f159dfd7b908e1c369a to your computer and use it in GitHub Desktop.
Save rodrigopolo/1f159dfd7b908e1c369a to your computer and use it in GitHub Desktop.
Human readable bandwith
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