Skip to content

Commit 578d61b

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 33a5305 + 11726c5 commit 578d61b

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

haxe/ui/util/StringUtil.hx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)