@@ -335,6 +335,29 @@ function getCardHeight(array $params): int
335335 return max ($ minimumHeight , intval ($ params ["card_height " ] ?? $ defaultHeight ));
336336}
337337
338+ /**
339+ * Format number using locale and short number if requested
340+ *
341+ * @param float $num The number to format
342+ * @param string $localeCode Locale code
343+ * @param bool $useShortNumbers Whether to use short numbers
344+ * @return string The formatted number
345+ */
346+ function formatNumber (float $ num , string $ localeCode , bool $ useShortNumbers ): string
347+ {
348+ $ numFormatter = new NumberFormatter ($ localeCode , NumberFormatter::DECIMAL );
349+ $ suffix = "" ;
350+ if ($ useShortNumbers ) {
351+ $ units = ["" , "K " , "M " , "B " , "T " ];
352+ for ($ i = 0 ; $ num >= 1000 ; $ i ++) {
353+ $ num /= 1000 ;
354+ }
355+ $ suffix = $ units [$ i ];
356+ $ num = round ($ num , 1 );
357+ }
358+ return $ numFormatter ->format ($ num ) . $ suffix ;
359+ }
360+
338361/**
339362 * Generate SVG output for a stats array
340363 *
@@ -362,9 +385,6 @@ function generateCard(array $stats, array $params = null): string
362385 // locale date formatter (used only if date_format is not specified)
363386 $ dateFormat = $ params ["date_format " ] ?? ($ localeTranslations ["date_format " ] ?? null );
364387
365- // number formatter
366- $ numFormatter = new NumberFormatter ($ localeCode , NumberFormatter::DECIMAL );
367-
368388 // read border_radius parameter, default to 4.5 if not set
369389 $ borderRadius = $ params ["border_radius " ] ?? 4.5 ;
370390
@@ -417,13 +437,15 @@ function generateCard(array $stats, array $params = null): string
417437 19.5 + $ heightOffset ,
418438 ];
419439
440+ $ useShortNumbers = ($ params ["short_numbers " ] ?? "" ) === "true " ;
441+
420442 // total contributions
421- $ totalContributions = $ numFormatter -> format ($ stats ["totalContributions " ]);
443+ $ totalContributions = formatNumber ($ stats ["totalContributions " ], $ localeCode , $ useShortNumbers );
422444 $ firstContribution = formatDate ($ stats ["firstContribution " ], $ dateFormat , $ localeCode );
423445 $ totalContributionsRange = $ firstContribution . " - " . $ localeTranslations ["Present " ];
424446
425447 // current streak
426- $ currentStreak = $ numFormatter -> format ($ stats ["currentStreak " ]["length " ]);
448+ $ currentStreak = formatNumber ($ stats ["currentStreak " ]["length " ], $ localeCode , $ useShortNumbers );
427449 $ currentStreakStart = formatDate ($ stats ["currentStreak " ]["start " ], $ dateFormat , $ localeCode );
428450 $ currentStreakEnd = formatDate ($ stats ["currentStreak " ]["end " ], $ dateFormat , $ localeCode );
429451 $ currentStreakRange = $ currentStreakStart ;
@@ -432,7 +454,7 @@ function generateCard(array $stats, array $params = null): string
432454 }
433455
434456 // longest streak
435- $ longestStreak = $ numFormatter -> format ($ stats ["longestStreak " ]["length " ]);
457+ $ longestStreak = formatNumber ($ stats ["longestStreak " ]["length " ], $ localeCode , $ useShortNumbers );
436458 $ longestStreakStart = formatDate ($ stats ["longestStreak " ]["start " ], $ dateFormat , $ localeCode );
437459 $ longestStreakEnd = formatDate ($ stats ["longestStreak " ]["end " ], $ dateFormat , $ localeCode );
438460 $ longestStreakRange = $ longestStreakStart ;
0 commit comments