How to add Letters in a counter with GDscript?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By BQKdev

I want to use letters to describe how much u got in Money.

Example:

I have a variable name Money

var money = 12345

And this is what i want to display

12345 to "1.2k"

How do I do it?

That’s going to take some custom code. Really, it shouldn’t be too difficult, but you’ll first need to define exactly how you’d expect ANY value to be displayed. So, think about that and come up with some rules that work for everything. Once you have those, converting that to code shouldn’t be too hard (depending on the details).

jgodfrey | 2023-01-22 05:07

:bust_in_silhouette: Reply From: Gluon

In principle this isnt difficult but you would need to plan out how you would want to display it for given amounts. If it was me the way I would do it would be to first create a string of the value

var money_string = str(money)

and after this you can base some logic on the length of your string for instance

if money_string.length() > 4:
	print(money_string.left(1) + "." + money_string.right(1) + "k")

would be one simplistic way to do this but may not be the way you want to show it. Try reading through the documentation here

and you can then plan out your logic.

The number in his example is 12345 and the expected display is 1.2k.
However if the number were 12999 would the expected display still be 1.2k?
I would think the number expected would be 1.3k.
Then there is the next level to consider. Would 1234567 be displayed as 1234.5k or 1.2m?
And the lower level. Does 123 get displayed as 0.1k, .1k, or as it is?
Certainly it is not unworkable in any case however it definitely needs planning out as you say.

LeslieS | 2023-01-23 17:12

Well you can use something like

	print(money_string.substr(0, 1)+"."+money_string.substr(1, 1)+"k")

but my point was that how he choses to make it look is up to him.

Gluon | 2023-01-23 17:14

Thank you for helping me, I will try out the code, it really help
I will see about how to use it.

BQKdev | 2023-01-23 22:03