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.
- 0, 999, 1000 and 1234567: both right, 0, 999, 1,000 and 1,234,567.
- -1000: both right, -1,000. -123456: the loop gave -,123,456, the script -123,456. -999: the loop gave -,999, the script -999.
- The largest and smallest int, 9,223,372,036,854,775,807 and -9,223,372,036,854,775,808: both right.
absi()of the smallest int gave back -9223372036854775808, still negative, which is why the script takes the digits fromstr()instead. - Floats through the loop: 1234.5 became 123,4.5, 1234567.891 became 12,345,67.,891, -1234.5 became -,123,4.5 and 999.999 became 9,99.,999.
- Floats through the script with 2 decimals: 1,234.50, 1,234,567.89, -1,234.50, 1,000.00 for 999.999, 5.00 for 5.0, 0.30 for 0.1 + 0.2, and 0.00 for -0.004, not -0.00.
- Other separators: 1.234.567, 1.234,50 and 1 234 567. With no decimals: 1,234,568.
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
- Numbers too big to read even with commas, like 8.2e177 gold in an idle game: big numbers with K, M and B.
- Percentages that show 0% or 100% too early: health bar percent. Gold earned while the game was closed: offline progress.
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
- A screen fade that finishes while the game is paused or slowed
- A hand of cards that always fits, without spreading out when it is small
- Edge markers that stay on the rim and point the true way
- A wrapped Label that is as wide as its text, up to a limit
- Show a 0 to 1 ratio on a progress bar, and fill a ring or gauge from the angle you want
- Blink or fade a tinted HUD element without losing its color
- Spin a HUD ring, radar or icon in place instead of around its corner
- Make buttons clickable again under a full-screen fade, transition or post-process ColorRect
- 9-slice window: corners that do not stretch
- Visual novel background: fill the screen, crossfade scenes
- Coins that fly to the HUD counter
- Floating damage numbers in 2D
- An icon over a 3D character's head, without the ghost behind the camera
- Drag a box to select 3D units, in any direction
- Radar blips that turn with the player
- Pick the radial menu slot the mouse is pointing at
- Show a percent that never says 0% or 100% too early
- Type dialogue at the same letters per second on every line and every frame rate
- Place brush dots at the same spacing however fast the mouse moves
- Move a speedometer or gauge needle smoothly without cutting through the bottom
- A countdown that never shows 0 while time is left
- A coin counter that rolls up and lands on the real total