Skip to content

Instantly share code, notes, and snippets.

@ctbarna
Created October 15, 2012 22:29
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 ctbarna/3896063 to your computer and use it in GitHub Desktop.
Save ctbarna/3896063 to your computer and use it in GitHub Desktop.
Round orders of magnitude in JS.
function m(n, d) {
P = Math.pow;
R = Math.round
d = P(10, d);
i = 7;
while(i) {
(s = P(10, i-- * 3)) <= n && (n = R(n * d / s) / d + "KMGTPE"[i])
}
return n;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment