[SOLVED BY ZEROX4] Formatting Numbers Easily (100,000 -> 100K)

Hey everyone! What’s the easiest way to format numbers like this? 1000000 → 1.0M? I know it is pretty simple to do this but I’m doing it many, many times in text labels and etc and was wondering if there is any easier way to do it. Thanks in advance!

– Snowy

SOLVED USING THIS EXTENSION:

TYSM ZeroX4!

Believe or not that search function in top right corner is actually working
And that is not sarcasm
I was sure it will be same as on other forums where it won’t find what im looking for
But NO it actually is pretty good in finding stuff
Try it sometimes

I did try it, and actually spotted that post earlier. I can’t seem to understand that extension very much :confused:

On that extension github page
There is example file which you can open in gdevelop
So you can totally look everything up that was made for it to work
And deduce what exactly you need

You could even create new scene in that project and just try to recreate it step by step

Option B
Make forum post asking can someone explain how to use it

Where i would strongly suggest trying that example
You need to learn how to deduce stuff from examples
This would be good lesson

I haven’t looked at the extension but I remember that Reborn loved using javascript. But if you’ve achieved something similar with events then you could just extract them to a function and just call the function each time.

1 Like

I’ve found a way to simplify it. I create all the events for a text label, then turn on a variable to update the text label. It works I guess, but someone should fr make an extension that’s more simple :smile:

1 Like

That’s great. Although I’m curious now about the extension, I’ll have a look at it later.

Later
I had a look, and it’s in normal events not javascript. I have to say my eyes glazed over when I saw the array and json stuff, I didn’t want to bother figuring it out.

1 Like

Same. I don’t understand the array and JSON part, can’t you just say “If number >= 1,000,000 then divide by 1 million, floor, and add M”?

Edit: The way I do it looks like “Cookies: “+ ToString(floor(Cookies/1000))+”.”+ToString(mod(floor(Cookies/100), 10))+“K”
(That’s for the thousands)

Edit: Oh boy, gotta repeat that for every text label in my game!

1 Like

Hi @SnowyRawrGamer!

I think it would be useful to create a function which will treat all possible cases.
I didn’t make a function in GD. So, i am not actually the good person to do that.
However, what i can say using logic expressions: (T=Thousands; M=Millions; B=Billions) is:

IF 1000 < number < 1 000 000 THEN print (number/1 000) + “T”
ELSE IF number < 1 000 000 000 THEN print (number/1 000 000) + “M”
ELSE IF number < 1 000 000 000 000 THEN print (number/1 000 000 000) + “B”
ELSE
print number

A short analys in my head.

A+
Xierra

@SnowyRawrGamer @Bubble
If extension is too hard then events will be easy

1 - THIS needs to be GLOBAL array variable

2 - LMB + keys 0 to 9 to add 1+That numbers of zeros for example holding 1 and LMB will add 10 and holding 3 and LMB will add 1000
Where same keys pressed while holding RMB will add 2x amount of zeros
Not holding LMB and RMB will subtract 1+that amount of zeros of which numeric key you are holding
Where holding both LMB and RMB + numeric key will subtract 1+TWICE that amount of zeros of numeric key you are holding
BUT this is only for my example you don’t need to do it

3 - This is what is in text change action

"NOT Formated "+FailVariable+"
Formated "+SubStr(FailVariable,0,1+mod(StrLength(FailVariable)-1,3))+" "+StrRepeat(Format[round((StrLength(FailVariable)-2)/3)-1],StrLength(SubStr(FailVariable,3,1)))+" "+"
Formated Decimal "+SubStr(FailVariable,0,1+mod(StrLength(FailVariable)-1,3))+StrRepeat("."+SubStr(FailVariable,3,1),StrLength(SubStr(FailVariable,3,1)))+" "+StrRepeat(Format[round((StrLength(FailVariable)-2)/3)-1],StrLength(SubStr(FailVariable,3,1)))

But this is for my example
You will actually want either this to display something like 12 M or 7 K

SubStr(FailVariable,0,1+mod(StrLength(FailVariable)-1,3))+" "+StrRepeat(Format[round((StrLength(FailVariable)-2)/3)-1],StrLength(SubStr(FailVariable,3,1)))

Or this to display something like 12.4 M or 7.2 K

SubStr(FailVariable,0,1+mod(StrLength(FailVariable)-1,3))+StrRepeat("."+SubStr(FailVariable,3,1),StrLength(SubStr(FailVariable,3,1)))+" "+StrRepeat(Format[round((StrLength(FailVariable)-2)/3)-1],StrLength(SubStr(FailVariable,3,1)))

4 - LAST PART replacing FailVariable
You just need to replace FailVariable with name of your variable
Formula without decimal have only 4 places where you need to put your variable
Formula with decimals have 6

Enjoy

@Bubble @SnowyRawrGamer

But then i just made extension for it

Which should be easier to use than other one and from my event system
If YourVariable = 1000
This will give you 1K

Formatter::FormatNumber(YourVariable,".", 0,"")

This will give you 1.0K

Formatter::FormatNumber(YourVariable,".", 1,"")
Holding LMB + 0-9 keys will add 1 and that many zeros
For example LMB + 3 will add 1 000
LMB + 1 adds 10
RMB is same story but 2x amount of zeros
RMB + 2 adds 2 0000
Not holding RMB and LMB while pressing 0-9 keys will SUBTRACT 1+ that many zeros as keys you pressed
Holding both RMB and LMB and then pressing numeric keys will subtract 1+ 2x that many zeros

Enjoy

3 Likes

HOLEY MOLEY ZEROX4 YOU ARE LITTERALLY THE GOAT :goat:
THIS IS SO HELPFUL AND WILL SAVE ME SO MUCH TIME EVEN THOUGH I HAVE ALMOST 0 IDEA OF HOW IT WORKS!

Thanks so much you are THE BEST!

Also quick question, if you send a GDevelop project to someone else, they still receive the extensions from it right? Just checking cause I might use this extension in a template :smile:

1 Like

Yeah although idk where they are stored
I would assume directly in your json project file

And to check how my extension works you can simply double click it in extension window
It uses very slightly modified version of what was in event version i posted earlier
I needed to modify it to work with extension itself

1 Like

Hey! Found a small bug with the extension:
If you have a bigger number such as 12K and you have 1 decimal point, the decimal point will always show the second number.
Example: 123.2K will never show 123.3K since the second number is 2. Or 75.5M will never show 75.6M or 75.4M cause the second number is 5.

I tried your extension Zero and it’s really good. It was all crazy looking to me too Snowy, haha, I would have no idea how to do that. I tested it with pressing the space bar to increase a number by a random amount. I gave it a pretty good work out but not precise enough to see what Snowy is talking about.

With the format by number, what is the second separator for? Like, what would that be used for?

My only feedback would be if, say, I had 1 decimal but then for numbers more than a million I wanted it to be 2 or more decimals instead so that it would register values more often. However, I think that’s a very small thing and I’d rather just have 2 decimals to begin with. And if it were my extension I’d do anything rather than spoil the perfect one action thing for each function.

I didn’t test the blinker function, but what a great idea. Oh, and you probably meant to have ColonON/OFF as the parameter names.

1 Like

All I tested was the format number. You probably would want the second separator if you wanted it to say something like 14.5 M with a space or 14.5-M whatever style your game is. Even if it probably won’t be used much it’s still a good feature to have.

1 Like

Just pinging @ZeroX4 so he will check this whenever he gets back online, is this format problem something you will be able to fix?

And what makes you think 1st time when you reply to my message did not give me any notification?

Re download and re install Formatter
Its now fixed
THX for spotting i totally did not even notice
And i added Formatter::DeviceTime()
Which will automatically spit out your device time
But its 24H time next step is to add to it choice for 12AM PM time

Holding LMB + 0-9 keys will add 1 and that many zeros
For example LMB + 3 will add 1 000
LMB + 1 adds 10
RMB is same story but 2x amount of zeros
RMB + 2 adds 2 0000
Not holding RMB and LMB while pressing 0-9 keys will SUBTRACT 1+ that many zeros as keys you pressed
Holding both RMB and LMB and then pressing numeric keys will subtract 1+ 2x that many zeros

@Bubble Look quick lesson of what how to make it
If you have Score variable that goes 0 1 2 3 4 5 6 7 8 9
So it adds 1 to it constantly
But you gonna use mod(Score,3) in text object then you it will loop around 3 digits
FOR EXAMPLE

Score
0 1 2 3 4 5 6 7 8 9
0 1 2 0 1 2 0 1 2 0
mod(Score,3)

Then there is StrLength(string) which returns number of characters in string
For example StrLength(wow) = 3
StrLength(formatter) = 9

Now imagine this if i want to use SubStr(String , starting position , portion)
To display part of that String i could set it to
SubStr(word,1,1) to display o but if i would have number like 123456
Then
SubStr(123456,1,1) = 2
But you can do it to variable that have some number
And what if i would do
SubStr(Score,0,1+mod(StrLength(Score),3))
Then we would see either 1 or 2 or 3 characters depending on length of Score variable
For example Score variable = 1 we will see 1
If 2 we see 2 if 3 we see 3
BUT if 4 we gonna see 1
For example
Score = 123
SubStr(Score,0,1+mod(StrLength(Score),3)) = 123
But
Score = 1234
SubStr(Score,0,1+mod(StrLength(Score),3)) = 1
Score = 45862
SubStr(Score,0,1+mod(StrLength(Score),3)) = 45

And everything you see in my extension works around stuff like that
https://wiki.gdevelop.io/gdevelop5/all-features/expressions-reference
This is far from perfect but a lot you can find out just by knowing expressions work and tying to use it in text object to get anything out of it

Snowy was right 2nd separator is for you to decide should there be space between number and suffix or - or nothing
I do not like deciding for user
i prefer to leave a choice
Look how suffix with my font in my example is absurdly close to number
Looks perfect for numbers themselves to be that close
But not for suffix that is why there is a choice
Same way that why separator have ON OFF instead only of ON
What if someone want it to blink between / and
Or < and > or ; and : or even maybe - and +

Anyway go to example i pasted in this post and look what happens (device time on bottom)
When your space character have different spacing than colon character
Now someone could be hey in my font # and ^ takes same space
Maybe i just gonna replace them to space and : in font file
And i would get blinking colon
or replace with whatever they want

And in parameter at first i wanted to put there comma that is why it was named comma but you made me realize it was wrong
Now its SeparatorOFF ON

I don’t understand your feedback
Write example when should happen what
Because i am pretty sure i still can keep it 1 action
OR maybe what you want maybe is doable trough expression itself outside extension
What i understand you would want
1.1K
1.122M
1.243B
1.245T

So you set decimal to 1 but its only 1 digit at 1k
But anything above million goes for 3?
Or million have 2 decimal places but then from Billion and up goes to 3?

1 Like

Thanks so much, the formatting works now! :smile:

1 Like

Yes, I think you’ve understood my feedback. If I have one decimal, once the number gets to 1.0M, it will stay on that until the number reaches 1.1M even though the number is still increasing.

I wasn’t arguing against a second separator, I just wondered what it was for and as Snowy explained, it makes sense to have one.

I’ll try the updated version later and have another look at all the subs string stuff.

1 Like