impr(about): showing decimal places for typing stats if the number is small

Also no longer always showing millions - order of magnitude is now dynamic.
This commit is contained in:
Miodec 2023-10-09 13:36:08 +01:00
parent cf2ff913eb
commit 876b882834
2 changed files with 53 additions and 6 deletions

View file

@ -56,19 +56,35 @@ function updateStatsAndHistogram(): void {
Math.round(secondsRounded / 3600) + " hours"
);
$(".pageAbout #totalStartedTestsStat .val").text(
Math.round(typingStatsResponseData.testsStarted / 1000000)
const startedWithMagnitude = Misc.getNumberWithMagnitude(
typingStatsResponseData.testsStarted
);
$(".pageAbout #totalStartedTestsStat .val").text(
typingStatsResponseData.testsStarted < 10
? startedWithMagnitude.roundedTo2
: startedWithMagnitude.rounded
);
$(".pageAbout #totalStartedTestsStat .valSmall").text(
startedWithMagnitude.orderOfMagnitude
);
$(".pageAbout #totalStartedTestsStat .valSmall").text("million");
$(".pageAbout #totalStartedTestsStat").attr(
"aria-label",
typingStatsResponseData.testsStarted + " tests"
);
$(".pageAbout #totalCompletedTestsStat .val").text(
Math.round(typingStatsResponseData.testsCompleted / 1000000)
const completedWIthMagnitude = Misc.getNumberWithMagnitude(
typingStatsResponseData.testsCompleted
);
$(".pageAbout #totalCompletedTestsStat .val").text(
typingStatsResponseData.testsCompleted < 10
? completedWIthMagnitude.roundedTo2
: completedWIthMagnitude.rounded
);
$(".pageAbout #totalCompletedTestsStat .valSmall").text(
completedWIthMagnitude.orderOfMagnitude
);
$(".pageAbout #totalCompletedTestsStat .valSmall").text("million");
$(".pageAbout #totalCompletedTestsStat").attr(
"aria-label",
typingStatsResponseData.testsCompleted + " tests"

View file

@ -1679,4 +1679,35 @@ export function updateTitle(title?: string): void {
}
}
export function getNumberWithMagnitude(num: number): {
rounded: number;
roundedTo2: number;
orderOfMagnitude: string;
} {
const units = [
"",
"thousand",
"million",
"billion",
"trillion",
"quadrillion",
"quintillion",
];
let unitIndex = 0;
let roundedNum = num;
while (roundedNum >= 1000) {
roundedNum /= 1000;
unitIndex++;
}
const unit = units[unitIndex];
return {
rounded: Math.round(roundedNum),
roundedTo2: roundTo2(roundedNum),
orderOfMagnitude: unit,
};
}
// DO NOT ALTER GLOBAL OBJECTSONSTRUCTOR, IT WILL BREAK RESULT HASHES