The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

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?

Godot version 3.4.5
in Engine by (39 points)

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).

1 Answer

+1 vote
Best answer

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

https://docs.godotengine.org/en/stable/classes/class_string.html?highlight=string

and you can then plan out your logic.

by (3,328 points)
selected by

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.

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.

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.