@@ -17,28 +17,33 @@ limitations under the License.
17
17
18
18
import { ReactElement , ReactNode } from "react" ;
19
19
20
- import { _t } from "../languageHandler" ;
20
+ import { _t , getCurrentLanguage } from "../languageHandler" ;
21
21
import { 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
+ } ) ;
22
29
23
30
/**
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, ...
26
33
*/
27
34
export 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 ) ;
34
36
}
35
37
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
+
36
42
/**
37
43
* Format a count showing the whole number but making it a bit more readable.
38
44
* e.g: 1000 => 1,000
39
45
*/
40
46
export function formatCountLong ( count : number ) : string {
41
- const formatter = new Intl . NumberFormat ( ) ;
42
47
return formatter . format ( count ) ;
43
48
}
44
49
0 commit comments