Make formatBigNumber() include a decimal point, and support numbers up to 999.9T.

This commit is contained in:
hexagonhexagon 2020-05-29 18:02:35 -04:00
parent 576bd3166f
commit a92d703395
1 changed files with 20 additions and 22 deletions

View File

@ -24,9 +24,7 @@ export const BOTTOM = new Vector(0, 1);
export const LEFT = new Vector(-1, 0); export const LEFT = new Vector(-1, 0);
export const ALL_DIRECTIONS = [TOP, RIGHT, BOTTOM, LEFT]; export const ALL_DIRECTIONS = [TOP, RIGHT, BOTTOM, LEFT];
export const thousand = 1000; const bigNumberSuffixes = ["k", "M", "B", "T"];
export const million = 1000 * 1000;
export const billion = 1000 * 1000 * 1000;
/** /**
* Returns the build id * Returns the build id
@ -435,21 +433,21 @@ export function formatBigNumber(num, divider = ".") {
if (num < 1000) { if (num < 1000) {
return sign + "" + num; return sign + "" + num;
} else {
let leadingDigits = num;
let suffix = "";
for (let suffixIndex = 0; suffixIndex < bigNumberSuffixes.length; ++suffixIndex) {
leadingDigits = leadingDigits / 1000;
suffix = bigNumberSuffixes[suffixIndex];
if (leadingDigits < 1000) {
break;
} }
if (num > 10000) {
return Math_floor(num / 1000.0) + "k";
} }
// round down to nearest 0.1
let rest = num; const leadingDigitsRounded = Math_floor(leadingDigits * 10) / 10;
let out = ""; const leadingDigitsNoTrailingDecimal = leadingDigitsRounded.toString().replace(".0", "");
return sign + leadingDigitsNoTrailingDecimal + suffix;
while (rest >= 1000) {
out = (rest % 1000).toString().padStart(3, "0") + (out !== "" ? divider : "") + out;
rest = Math_floor(rest / 1000);
} }
out = rest + divider + out;
return sign + out;
} }
/** /**