@@ -50,6 +50,51 @@ pub fn format_bytes(bytes: u32) -> String {
50
50
}
51
51
}
52
52
53
+ /// Formats a number with "k" and "M" suffixes for thousands and millions.
54
+ ///
55
+ /// The function follows these rules:
56
+ /// - Uses suffixes: none, k, and M
57
+ /// - Switches from no suffix to k at 1500
58
+ /// - Switches from k to M at 1500 * 1000
59
+ /// - Limits the number to a maximum of 4 characters by adjusting decimal places
60
+ ///
61
+ /// # Arguments
62
+ ///
63
+ /// * `number` - The number to format
64
+ ///
65
+ /// # Returns
66
+ ///
67
+ /// A formatted string representing the number with appropriate suffixes
68
+ pub fn format_number ( number : u32 ) -> String {
69
+ const THRESHOLD : f64 = 1500. ;
70
+ const UNITS : & [ & str ] = & [ "" , "k" , "M" ] ;
71
+
72
+ let mut value = number as f64 ;
73
+ let mut unit_index = 0 ;
74
+
75
+ // Keep dividing by 1000 until value is below threshold or we've reached the last unit
76
+ while value >= THRESHOLD && unit_index < UNITS . len ( ) - 1 {
77
+ value /= 1000.0 ;
78
+ unit_index += 1 ;
79
+ }
80
+
81
+ let unit = UNITS [ unit_index] ;
82
+
83
+ // Special case for numbers without suffix - no decimal places
84
+ if unit_index == 0 {
85
+ return format ! ( "{}" , number) ;
86
+ }
87
+
88
+ // For k and M, format with appropriate decimal places
89
+
90
+ // Determine number of decimal places to keep number under 4 chars
91
+ if value < 10.0 {
92
+ format ! ( "{:.1}{}" , value, unit)
93
+ } else {
94
+ format ! ( "{:.0}{}" , value, unit)
95
+ }
96
+ }
97
+
53
98
#[ cfg( test) ]
54
99
mod tests {
55
100
use super :: * ;
@@ -81,4 +126,32 @@ mod tests {
81
126
assert_eq ! ( format_bytes( 104857600 ) , "100 MB" ) ;
82
127
assert_eq ! ( format_bytes( 1073741824 ) , "1024 MB" ) ;
83
128
}
129
+
130
+ #[ test]
131
+ fn test_format_number ( ) {
132
+ // Test numbers without suffix (below 1500)
133
+ assert_eq ! ( format_number( 0 ) , "0" ) ;
134
+ assert_eq ! ( format_number( 1 ) , "1" ) ;
135
+ assert_eq ! ( format_number( 1000 ) , "1000" ) ;
136
+ assert_eq ! ( format_number( 1499 ) , "1499" ) ;
137
+
138
+ // Test numbers with k suffix (1500 to 1500 * 1000)
139
+ assert_eq ! ( format_number( 1500 ) , "1.5k" ) ;
140
+ assert_eq ! ( format_number( 2000 ) , "2.0k" ) ;
141
+ assert_eq ! ( format_number( 5000 ) , "5.0k" ) ;
142
+ assert_eq ! ( format_number( 10000 ) , "10k" ) ;
143
+ assert_eq ! ( format_number( 50000 ) , "50k" ) ;
144
+ assert_eq ! ( format_number( 100000 ) , "100k" ) ;
145
+ assert_eq ! ( format_number( 500000 ) , "500k" ) ;
146
+ assert_eq ! ( format_number( 999999 ) , "1000k" ) ;
147
+
148
+ // Test numbers with M suffix (above 1500 * 1000)
149
+ assert_eq ! ( format_number( 1500000 ) , "1.5M" ) ;
150
+ assert_eq ! ( format_number( 2000000 ) , "2.0M" ) ;
151
+ assert_eq ! ( format_number( 5000000 ) , "5.0M" ) ;
152
+ assert_eq ! ( format_number( 10000000 ) , "10M" ) ;
153
+ assert_eq ! ( format_number( 50000000 ) , "50M" ) ;
154
+ assert_eq ! ( format_number( 100000000 ) , "100M" ) ;
155
+ assert_eq ! ( format_number( 1000000000 ) , "1000M" ) ;
156
+ }
84
157
}
0 commit comments