Skip to content

Instantly share code, notes, and snippets.

@clementi
Created September 3, 2010 18:56
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/564367 to your computer and use it in GitHub Desktop.
Save clementi/564367 to your computer and use it in GitHub Desktop.
var prefixes = [
{
prefix: "",
name: ""
},
{
prefix: "Ki",
name: "kibi"
},
{
prefix: "Mi",
name: "mebi"
},
{
prefix: "Gi",
name: "gibi"
},
{
preifx: "Ti",
name: "tebi"
},
{
prefix: "Pi",
name: "pebi"
},
{
prefix: "Ei",
name: "exbi"
}
];
function binaryUnitFormat(x, unit) {
var index = 0;
var xnew = x;
while (xnew >= 1024) {
xnew /= 1024;
index++;
}
var result = {};
result.mag = xnew;
result.unit = prefixes[index].prefix + unit;
return result;
}
var count = 1138762;
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