fix: significant digits bug fix

This commit is contained in:
NunoSempere 2022-05-24 14:46:21 -04:00
parent 307bc4990b
commit b09e9f364d

View File

@ -37,14 +37,35 @@ export function numToAlphabeticalString(num) {
return `#${s}` || undefined;
}
let topOutAt100AndValidate = (x) => {
// return 10;
if (x == x) {
return x > 99 ? 99 : x < 0 ? 2 : x;
} else {
return 10;
}
};
export function formatLargeOrSmall(num) {
let result;
if (num >= 1) {
result = toLocale(truncateValueForDisplay(num));
} else if (num > 0) {
result = num.toFixed(-Math.floor(Math.log(num) / Math.log(10)) + 1);
} else if (num < -1) {
result = num.toFixed(-Math.floor(Math.log(-num) / Math.log(10)) + 1);
let candidateNumSignificantDigits =
-Math.floor(Math.log(num) / Math.log(10)) + 1;
let numSignificantDigits = topOutAt100AndValidate(
candidateNumSignificantDigits
);
result = num.toFixed(numSignificantDigits);
} else if (-1 < num) {
let candidateNumSignificantDigits =
-Math.floor(Math.log(Math.abs(num)) / Math.log(10)) + 1;
let numSignificantDigits = topOutAt100AndValidate(
candidateNumSignificantDigits
);
result = num.toFixed(numSignificantDigits);
} else if (num <= -1) {
result = "-" + toLocale(truncateValueForDisplay(-num));
} else {
result = toLocale(num); //return "~0"
}