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; 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) { export function formatLargeOrSmall(num) {
let result; let result;
if (num >= 1) { if (num >= 1) {
result = toLocale(truncateValueForDisplay(num)); result = toLocale(truncateValueForDisplay(num));
} else if (num > 0) { } else if (num > 0) {
result = num.toFixed(-Math.floor(Math.log(num) / Math.log(10)) + 1); let candidateNumSignificantDigits =
} else if (num < -1) { -Math.floor(Math.log(num) / Math.log(10)) + 1;
result = num.toFixed(-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 { } else {
result = toLocale(num); //return "~0" result = toLocale(num); //return "~0"
} }