[4.2 stable] How to shorten numbers using Technical fields

Godot Version

4.2 Stable


Hello Godot Forum. I started out Godot not long ago for a school project and I am having alot of fun. My game is sort of like a clicker game without a clicker. If you have ever played Earth Inc the mobile game i guess its sort of similar to that.

What I am trying to achieve is when you get an unbelievable ammount of coins I want to shorten it using Technical fields (According to google Gemini). Technical fields is the letter after the number in a big number. For example, 10,000 would equal to 10K. 104,000 would equal to 104K. Although when I get to the smaller number places. I want it to show a decimal. so for example, 1,045,400 would equal to 1.04M. I want this to be shortened to 2 decimal places no matter what.

I may be guessing the code could go into the _proccess function but idk

Any response would be greatly appreciated! :smile:

  • sebashtioon

Good morning :). I thought a use of a snapped() would work here, but thinking about a logic feature I don’t think so. Maybe a more manual job, something like:

extends Node

func _ready():
    var number = 1_000_000
    var formated_number = format_number(numero)
    print(numero_formatado)  # output: "1kk"

func format_number(n: int) -> String:
    if n >= 1_000_000:
        return str(float(n) / 1_000_000).replace(",", ".") + "kk"
    elif n >= 1_000:
        return str(float(n) / 1_000).replace(",", ".") + "k"
        return str(n)

The Code was made by AI, but I don’t think there’s anything wrong here, if there is you can tell me : )

1 Like

Where would I call format_number()? in process()?

Also, would n be the ammount of coins?

In your _process or whenever you update the currency value, without having an idea of how your script is doing it’s hard to tell. And yes, N is the amount of coins (even this script only works up to 1kk so then you’ll have to use logic and write up to which one you want)

One thing that I think is wrong with this is it only shortens trailing 0’s.
For example 1000 gets shortened to 1k but 1234 gets “shortened” to 1.234k (which is actually longer). One would prefer 1234 be 1.2k and in the case of 1256 be 1.3k.

The snapped() function seems to help us here:

 func format_number(n: int) -> String:
	if n >= 1_000_000:
		var i:float = snapped(float(n)/1000000, .01)
		return str(i).replace(",", ".") + "kk"
	elif n >= 1_000:
		var i:float = snapped(float(n)/1000, .01)
		return str(i).replace(",", ".") + "k"
		return str(n)
1 Like

Hey this code looks like it works… although I am unsure of what “kk” is supposed to mean. Could you tell me how I would do this for Millions, Trillions, or just maybe suggest a system I could use (if you know what I mean)? Sorry im not exactly experienced at Godot could you explain it in simpler terms? Z ╥﹏╥

1 Like

The “kk” case is how you would do this for millions (since 1 000 x 1 000 = 1 000 000). The logic is always the same, you only have to change two numbers to create a new case:

 func format_number(n: int) -> String:
	if n >= 1_000_000_000_000:
		# ran for every number <n> greater or equal to a trillion
		var i:float = snapped(float(n)/1_000_000_000_000, .01)
		return str(i).replace(",", ".") + "B"
	elif n >= 1_000_000:
		# ran for every number <n> smaller than 1 trillion BUT
		# still greater or equal to 1 million
		var i:float = snapped(float(n)/1_000_000, .01)
		return str(i).replace(",", ".") + "M"
	elif n >= 1_000:
		# ran for every number <n> smaller than 1 million BUT
		# still greater or equal to 1 thousand
		var i:float = snapped(float(n)/1_000, .01)
		return str(i).replace(",", ".") + "k"
		# ran otherwise
		return str(n)

(The underscores in the numbers are optional, they just make it easier to read)


Would I need to set n as my amount of coins? Because otherwise n woudn’t have a value.

“n = coins” just under function declaration?

How can I directly set this value to the coins variable in my script?

If your coins variable is a numeric variable then you can’t directly have it carry the formatting. Numeric variables can only contain numbers.
Its only when you print out a variable that you can change its formatting.
If you want to set a label to show the number of coins:
my_coin_label.text = format_number(coins)

It seems though that you need to understand the basics of programming a little better.
Muddling through can only get you so far. I recommend reviewing a GDScript tutorial.


Thank you everyone :slight_smile: