fix: significant digits bug fix
This commit is contained in:
parent
307bc4990b
commit
b09e9f364d
27
lib/utils.js
27
lib/utils.js
|
@ -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"
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user