← Pixel Dreamer Studio

Big numbers with thousands separators, minus signs and all

Group only the digits, then put the sign and the decimals back.

Godot does not add thousands separators for you, so most games add the commas themselves, often with a loop that puts a comma every three characters from the right. In our Godot 4.7 test that loop wrote -123456 as -,123,456 and -999 as -,999, because it counts the minus sign as a digit, and turned 1234.5 into 123,4.5 and 1234567.891 into 12,345,67.,891. Grouping only the digits, then adding the sign and the decimals back, got every case right.

The code

Save it as group_digits.gd, load it with const Digits := preload("res://group_digits.gd") and set your label with gold_label.text = Digits.int_text(gold), or Digits.float_text(price) for two decimals. For players who write 1.234.567,50, pass the separators: Digits.float_text(price, 2, ".", ",").

extends RefCounted
## Writes numbers with thousands separators: 1234567 -> 1,234,567.
## Handles minus signs, decimals, the smallest int, and other separators.
##   Digits.int_text(-1234567)                  # -1,234,567
##   Digits.float_text(1234.5)                  # 1,234.50
##   Digits.int_text(1234567, ".")              # 1.234.567
##   Digits.float_text(1234.5, 2, ".", ",")     # 1.234,50


static func int_text(n: int, sep := ",") -> String:
	var digits := str(n).trim_prefix("-")  # str(), not absi(): absi() of the smallest int stays negative
	return ("-" if n < 0 else "") + _group(digits, sep)


static func float_text(x: float, decimals := 2, sep := ",", point := ".") -> String:
	var s := String.num(absf(x), decimals)
	if decimals > 0 and not "." in s:
		s += "." + "0".repeat(decimals)  # String.num(5.0, 2) gives "5"
	var parts := s.split(".")
	var frac := parts[1].rpad(decimals, "0") if parts.size() > 1 else ""
	var neg := x < 0.0 and s.trim_prefix("0").replace(".", "").replace("0", "") != ""  # no "-0.00"
	return ("-" if neg else "") + _group(parts[0], sep) + (point + frac if decimals > 0 else "")


static func _group(digits: String, sep: String) -> String:
	var out := ""
	while digits.length() > 3:
		out = sep + digits.substr(digits.length() - 3) + out
		digits = digits.substr(0, digits.length() - 3)
	return digits + out

What we measured

The same numbers through a comma-every-three-characters loop and through the script. Two runs gave the same results.

Why it happens

str() of a number is just text, and a loop over that text cannot tell a digit from a minus sign or a decimal point. When the length before the sign is a multiple of three, the comma lands between the sign and the first digit. With a float the decimal point and the digits after it are counted as well, so the groups start in the wrong place. The script takes the sign off, splits off the decimals, groups the whole part, and puts both back.

What to change

A whole idle sect game to put the numbers in: the Xianxia Sect Management System for Godot 4 is an idle sect game system: disciples with jobs, 9 buildings, 8 missions, random events, reputation, save/load and offline progress.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Xianxia Sect Management System for Godot 4 is an idle sect game system: disciples with jobs, 9 buildings, 8 missions, random events, reputation, save/load and offline progress.

More how-tos for Xianxia Sect Management System for Godot 4: Offline progress that counts the time the game was closed · Remove items from an Array while checking them, without skipping any

More how-tos: UI and HUD