Skip to content

Instantly share code, notes, and snippets.

@clementi
Created September 7, 2010 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clementi/568379 to your computer and use it in GitHub Desktop.
Save clementi/568379 to your computer and use it in GitHub Desktop.
var prefixes = [ "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei" ];
const GROUP = 1024;
function binaryUnitFormat(x, unit) {
var index = 0;
var xnew = x;
while (xnew >= GROUP && index < prefixes.length - 1) {
xnew /= GROUP;
index++;
}
var result = {};
result.mag = xnew;
result.unit = prefixes[index] + unit;
return result;
}
var count = 672635;
var unit = "B";
var formatted = binaryUnitFormat(count, unit);
print(count + " " + unit + " is about " + formatted.mag.toFixed(2) + " "
+ formatted.unit);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment