File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -142,5 +142,43 @@ class StringUtil {
142142
143143 return s ;
144144 }
145+
146+ public static function standardNotationSuffix (n : Float ) {
147+ var a = Math .abs (n );
148+ var i = n ;
149+ var suffix = " " ;
150+ if (a >= 0 && a < THOUSAND ) {
151+ suffix = " " ;
152+ } else if (a >= THOUSAND && a < MILLION ) {
153+ suffix = " K" ;
154+ } else if (a >= MILLION && a < BILLION ) {
155+ suffix = " M" ;
156+ } else {
157+ suffix = " B" ;
158+ }
159+ return suffix ;
160+ }
161+
162+ public static function valueForSuffix (n : Float , suffix : String ) {
163+ return switch (suffix ) {
164+ case " K" : n / THOUSAND ;
165+ case " M" : n / MILLION ;
166+ case " B" : n / BILLION ;
167+ case _ : n ;
168+ }
169+ }
170+
171+ /**
172+ * Formats number in standard notation, the step defines the precision
173+ * If the step is 250K, for 1250000 it will be 1.25 M and not rounded 1.3 M
174+ **/
175+ public static function formatNumberForStep (n : Float , step : Float ) {
176+ // It means the step shouldn't be rounded
177+ // If step is 250K It should be be 1.25 M and not rounded 1.3 M or whatever.
178+ var suffix = standardNotationSuffix (n );
179+ var stepValueForSuffix = valueForSuffix (step , suffix );
180+ var precision = MathUtil .precision (stepValueForSuffix );
181+ return formatNumber (n , precision );
182+ }
145183 #end
146184}
You can’t perform that action at this time.
0 commit comments