Skip to content

Commit 07fc7aa

Browse files
authored
Merge pull request #673 from Shallowmallow/FormatNumberFix
Non regex way to for decimal part + locale decimalSeparator
2 parents ebdc0d9 + 77ae8e5 commit 07fc7aa

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

haxe/ui/util/StringUtil.hx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class StringUtil {
8787
}
8888

8989
#if !macro // stringtools gets used in macros, but some functions rely on haxeui "bits" (like locales), lets wrap to avoid problems
90-
private static var humanReadableRegex = ~/\B(?=(\d{3})+(?!\d))(?<!\.\d*)/g;
90+
private static var humanReadableRegex = ~/\B(?=(\d{3})+(?!\d))/g;
9191
private static inline var THOUSAND:Int = 1000;
9292
private static inline var MILLION:Int = THOUSAND * THOUSAND;
9393
private static inline var BILLION:Int = MILLION * THOUSAND;
@@ -122,13 +122,22 @@ class StringUtil {
122122
var p = s.indexOf(".");
123123
if (p == -1 && precision > 0) {
124124
p = s.length;
125-
s += ".";
125+
s += haxe.ui.locale.Formats.decimalSeparator;
126126
}
127127
s = StringTools.rpad(s, "0", p + precision + 1);
128128
}
129129
s += suffix;
130130
} else {
131-
s = humanReadableRegex.replace(s, haxe.ui.locale.Formats.thousandsSeparator);
131+
var decimalSeparatorIndex = s.indexOf(".");
132+
if (decimalSeparatorIndex == -1) {
133+
s = humanReadableRegex.replace(s, haxe.ui.locale.Formats.thousandsSeparator);
134+
} else {
135+
var integerPart = s.substring(0, s.indexOf("."));
136+
var decimalPart = s.substring(s.indexOf(".") + 1);
137+
s = humanReadableRegex.replace(integerPart, haxe.ui.locale.Formats.thousandsSeparator);
138+
s += haxe.ui.locale.Formats.decimalSeparator;
139+
s += decimalPart;
140+
}
132141
}
133142

134143
return s;

0 commit comments

Comments
 (0)