@@ -335,6 +335,29 @@ function getCardHeight(array $params): int
335
335
return max ($ minimumHeight , intval ($ params ["card_height " ] ?? $ defaultHeight ));
336
336
}
337
337
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
+
338
361
/**
339
362
* Generate SVG output for a stats array
340
363
*
@@ -362,9 +385,6 @@ function generateCard(array $stats, array $params = null): string
362
385
// locale date formatter (used only if date_format is not specified)
363
386
$ dateFormat = $ params ["date_format " ] ?? ($ localeTranslations ["date_format " ] ?? null );
364
387
365
- // number formatter
366
- $ numFormatter = new NumberFormatter ($ localeCode , NumberFormatter::DECIMAL );
367
-
368
388
// read border_radius parameter, default to 4.5 if not set
369
389
$ borderRadius = $ params ["border_radius " ] ?? 4.5 ;
370
390
@@ -417,13 +437,15 @@ function generateCard(array $stats, array $params = null): string
417
437
19.5 + $ heightOffset ,
418
438
];
419
439
440
+ $ useShortNumbers = ($ params ["short_numbers " ] ?? "" ) === "true " ;
441
+
420
442
// total contributions
421
- $ totalContributions = $ numFormatter -> format ($ stats ["totalContributions " ]);
443
+ $ totalContributions = formatNumber ($ stats ["totalContributions " ], $ localeCode , $ useShortNumbers );
422
444
$ firstContribution = formatDate ($ stats ["firstContribution " ], $ dateFormat , $ localeCode );
423
445
$ totalContributionsRange = $ firstContribution . " - " . $ localeTranslations ["Present " ];
424
446
425
447
// current streak
426
- $ currentStreak = $ numFormatter -> format ($ stats ["currentStreak " ]["length " ]);
448
+ $ currentStreak = formatNumber ($ stats ["currentStreak " ]["length " ], $ localeCode , $ useShortNumbers );
427
449
$ currentStreakStart = formatDate ($ stats ["currentStreak " ]["start " ], $ dateFormat , $ localeCode );
428
450
$ currentStreakEnd = formatDate ($ stats ["currentStreak " ]["end " ], $ dateFormat , $ localeCode );
429
451
$ currentStreakRange = $ currentStreakStart ;
@@ -432,7 +454,7 @@ function generateCard(array $stats, array $params = null): string
432
454
}
433
455
434
456
// longest streak
435
- $ longestStreak = $ numFormatter -> format ($ stats ["longestStreak " ]["length " ]);
457
+ $ longestStreak = formatNumber ($ stats ["longestStreak " ]["length " ], $ localeCode , $ useShortNumbers );
436
458
$ longestStreakStart = formatDate ($ stats ["longestStreak " ]["start " ], $ dateFormat , $ localeCode );
437
459
$ longestStreakEnd = formatDate ($ stats ["longestStreak " ]["end " ], $ dateFormat , $ localeCode );
438
460
$ longestStreakRange = $ longestStreakStart ;
0 commit comments