Merge pull request #43 from hexagonhexagon/number-formatting

Better big number formatting
This commit is contained in:
tobspr 2020-06-01 13:06:22 +02:00 committed by GitHub
commit 356bb2b73a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 21 deletions

View File

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

View File

@ -26,6 +26,13 @@ global:
# How big numbers are rendered, e.g. "10,000"
thousandsDivider: ","
# The suffix for large numbers, e.g. 1.3k, 400.2M, etc.
suffix:
thousands: k
millions: M
billions: B
trillions: T
# Shown for infinitely big numbers
infinite: inf