Skip to content

Commit

Permalink
Round Human friendly numbers less than 1000
Browse files Browse the repository at this point in the history
When human friendly flag is true numbers less than 1k (1000) are not rounded.

Ex: n = 123.456789 d = 2  
Output: 123.456789  <--- Error
Expected: 123.45
  • Loading branch information
robertsLando authored Jan 11, 2019
1 parent 36a3149 commit 83c8a93
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions justgage.js
Original file line number Diff line number Diff line change
Expand Up @@ -1083,20 +1083,19 @@ function cutHex(str) {
return (str.charAt(0) == "#") ? str.substring(1, 7) : str;
}

/** Human friendly number suffix - From: http://stackoverflow.com/questions/2692323/code-golf-friendly-number-abbreviator */
/** Human friendly number suffix - @robertsLando */
function humanFriendlyNumber(n, d) {
var p, d2, i, s;

p = Math.pow;
d2 = p(10, d);
i = 7;
while (i) {
s = p(10, i-- * 3);
if (s <= n) {
n = Math.round(n * d2 / s) / d2 + "KMGTPE" [i];
}
}
return n;
var d2, i, s;

d2 = Math.pow(10, d);
s = " KMGTPE";
i = 0;

while(n >= 1000 && ++i < s.length) n = n / 1000;

i = i >= s.length ? s.length - 1 : i;

return Math.round(n * d2) / d2 + s[i];
}

/** Format numbers with commas - From: http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript */
Expand Down

0 comments on commit 83c8a93

Please sign in to comment.