In lieu of a decimal formatter or a regex replace function in Godot, you can implement your own helper function in a singleton by casting the number to a string and using the modulo operator to insert a comma for every three characters.
Something like this:
func comma_sep(number):
var string = str(number)
var mod = string.length() % 3
var res = ""
for i in range(0, string.length()):
if i != 0 && i % 3 == mod:
res += ","
res += string[i]
return res
It will need some tweaking depending on the format of the numbers you want to support though.
Thank you.
I thought that Godot had a fuction to do this for me, like the format() thing in python.
ZeBirosca | 2017-10-03 23:20
Yeah, in Python it would be as simple as "{:,}".format(number), if memory serves. Not so in Godot, unfortunately.
Here’s a more concise form of the comma_sep function which I use in my game:
static func comma_sep(n: int) -> String:
var result := ""
var i: int = abs(n)
while i > 999:
result = ",%03d%s" % [i % 1000, result]
i /= 1000
return "%s%s%s" % ["-" if n < 0 else "", i, result]