Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
LeaderboardType,
} from "@/types/scoring";
import cn from "@/utils/core/cn";
import { abbreviatedNumber } from "@/utils/formatters/number";
import { formatLeaderboardNumber } from "@/utils/formatters/number";
import { getMarkdownSummary } from "@/utils/markdown";
import { isUnsuccessfullyResolved } from "@/utils/questions/resolution";

Expand Down Expand Up @@ -142,7 +142,7 @@ const ContributionsTable: FC<Props> = ({
{ "w-20": isQuestionCategory }
)}
>
{abbreviatedNumber(totalScore, 4, false)}
{formatLeaderboardNumber(totalScore)}
</InfoHeaderTd>
{leaderboardType === "peer_global" && (
<InfoHeaderTd className="w-24 font-medium leading-4 " />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
LeaderboardType,
} from "@/types/scoring";
import cn from "@/utils/core/cn";
import { abbreviatedNumber } from "@/utils/formatters/number";
import { formatLeaderboardNumber } from "@/utils/formatters/number";
import { formatUsername } from "@/utils/formatters/users";

import MedalIcon from "../../../components/medal_icon";
Expand Down Expand Up @@ -110,7 +110,7 @@ const LeaderboardRow: FC<Props> = ({
className="flex items-center justify-end px-4 py-2.5 text-sm no-underline"
prefetch={false}
>
{abbreviatedNumber(contribution_count, 3, false)}
{formatLeaderboardNumber(contribution_count)}
</Link>
</td>
{scoreType == "peer_global" && (
Expand All @@ -120,7 +120,7 @@ const LeaderboardRow: FC<Props> = ({
className="flex items-center justify-end px-4 py-2.5 text-sm no-underline"
prefetch={false}
>
{abbreviatedNumber(coverage, 3, false)}
{formatLeaderboardNumber(coverage)}
</Link>
</td>
)}
Expand All @@ -135,7 +135,7 @@ const LeaderboardRow: FC<Props> = ({
className="flex items-center justify-end px-4 py-2.5 text-sm no-underline"
prefetch={false}
>
{abbreviatedNumber(score, 3, false)}
{formatLeaderboardNumber(score)}
</Link>
</td>
</tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { FC, PropsWithChildren } from "react";

import { LeaderboardEntry } from "@/types/scoring";
import cn from "@/utils/core/cn";
import { formatLeaderboardNumber } from "@/utils/formatters/number";
import { formatUsername } from "@/utils/formatters/users";

import MedalIcon from "../../../components/medal_icon";
Expand Down Expand Up @@ -85,12 +86,14 @@ const TableRow: FC<Props> = ({
</Link>
</Td>
<Td className="text-right tabular-nums" highlight={highlight}>
{score.toFixed(3)}
{formatLeaderboardNumber(score, 3)}
</Td>
{isAdvanced && (
<>
<Td className="text-right tabular-nums" highlight={highlight}>
{contribution_count ? `${contribution_count.toFixed(0)}` : "-"}
{contribution_count
? formatLeaderboardNumber(contribution_count)
: "-"}
</Td>
<Td className="text-right tabular-nums" highlight={highlight}>
{coveragePercent}
Expand All @@ -110,7 +113,7 @@ const TableRow: FC<Props> = ({
</>
)}
<Td className="text-right tabular-nums" highlight={highlight}>
{prize && prize >= 10 ? "$" + prize.toFixed(0) : "-"}
{prize && prize >= 10 ? "$" + formatLeaderboardNumber(prize) : "-"}
</Td>
</>
)}
Expand Down
33 changes: 33 additions & 0 deletions front_end/src/utils/formatters/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,36 @@ export function formatNumberWithUnit(
}
return `${formattedNumber} ${unit}`;
}

export function formatLeaderboardNumber(
val: number | string,
decimals: number = 0
): string {
const num = +val;

if (!isFinite(num)) {
return "0";
}

// Format the number with the specified decimal places
const fixed = num.toFixed(decimals);

// Split into integer and decimal parts
const parts = fixed.split(".");
const integerPart = parts[0] || "0";
const decimalPart = parts[1];

// Add thin space separators every 3 digits from the right
// U+2009 is the thin space character per BIPM standards
const formattedInteger = integerPart.replace(
/\B(?=(\d{3})+(?!\d))/g,
"\u2009"
);

// Combine with decimal part if it exists and has non-zero digits
if (decimalPart && parseInt(decimalPart) !== 0) {
return `${formattedInteger}.${decimalPart}`;
}

return formattedInteger;
}