@@ -17,28 +17,33 @@ limitations under the License.
1717
1818import { ReactElement , ReactNode } from "react" ;
1919
20- import { _t } from "../languageHandler" ;
20+ import { _t , getCurrentLanguage } from "../languageHandler" ;
2121import { jsxJoin } from "./ReactUtils" ;
22+ const locale = getCurrentLanguage ( ) ;
23+
24+ // It's quite costly to instanciate `Intl.NumberFormat`, hence why we do not do
25+ // it in every function call
26+ const compactFormatter = new Intl . NumberFormat ( locale , {
27+ notation : "compact" ,
28+ } ) ;
2229
2330/**
24- * formats numbers to fit into ~3 characters, suitable for badge counts
25- * e.g: 999, 9.9K , 99K, 0.9M, 9.9M , 99M, 0.9B, 9.9B
31+ * formats and rounds numbers to fit into ~3 characters, suitable for badge counts
32+ * e.g: 999, 10K , 99K, 1M, 10M , 99M, 1B, 10B, ...
2633 */
2734export function formatCount ( count : number ) : string {
28- if ( count < 1000 ) return count . toString ( ) ;
29- if ( count < 10000 ) return ( count / 1000 ) . toFixed ( 1 ) + "K" ;
30- if ( count < 100000 ) return ( count / 1000 ) . toFixed ( 0 ) + "K" ;
31- if ( count < 10000000 ) return ( count / 1000000 ) . toFixed ( 1 ) + "M" ;
32- if ( count < 100000000 ) return ( count / 1000000 ) . toFixed ( 0 ) + "M" ;
33- return ( count / 1000000000 ) . toFixed ( 1 ) + "B" ; // 10B is enough for anyone, right? :S
35+ return compactFormatter . format ( count ) ;
3436}
3537
38+ // It's quite costly to instanciate `Intl.NumberFormat`, hence why we do not do
39+ // it in every function call
40+ const formatter = new Intl . NumberFormat ( locale ) ;
41+
3642/**
3743 * Format a count showing the whole number but making it a bit more readable.
3844 * e.g: 1000 => 1,000
3945 */
4046export function formatCountLong ( count : number ) : string {
41- const formatter = new Intl . NumberFormat ( ) ;
4247 return formatter . format ( count ) ;
4348}
4449
0 commit comments