my website at ewancroft.uk
6
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix(numbers): floor compact number formatting instead of rounding

Large numbers (1K+, 1M+, etc.) were being rounded using default locale behaviour,
which could display inflated stats (e.g. 999.6 → 1K). The formatter now floors
values to one decimal place before applying compact notation.

+18
+18
src/lib/utils/formatNumber.ts
··· 16 16 /** 17 17 * Formats large numbers into a compact, human-readable format. 18 18 * Automatically adapts to the given or system locale. 19 + * Numbers are rounded DOWN to ensure stats don't appear inflated. 19 20 * @param num - The number to format 20 21 * @param locale - Optional locale string (defaults to system or 'en-GB') 21 22 * @returns Formatted string (e.g., "1.2K", "3.4M") ··· 24 25 if (num === undefined || num === null) return '0'; 25 26 const effectiveLocale = getLocale(locale); 26 27 28 + // For numbers >= 1000, round down to one decimal place for the compact format 29 + if (num >= 1000) { 30 + // Determine the divisor (1000 for K, 1000000 for M, etc.) 31 + const divisor = num >= 1000000000 ? 1000000000 : num >= 1000000 ? 1000000 : 1000; 32 + // Floor to one decimal place: floor(num / divisor * 10) / 10 33 + const roundedDown = Math.floor((num / divisor) * 10) / 10; 34 + // Re-multiply to get the actual number to format 35 + const adjustedNum = roundedDown * divisor; 36 + 37 + return new Intl.NumberFormat(effectiveLocale, { 38 + notation: 'compact', 39 + compactDisplay: 'short', 40 + maximumFractionDigits: 1 41 + }).format(adjustedNum); 42 + } 43 + 44 + // For numbers < 1000, just return as-is 27 45 return new Intl.NumberFormat(effectiveLocale, { 28 46 notation: 'compact', 29 47 compactDisplay: 'short',