Large Number Abbreviations [GameMaker: Studio]

In this tutorial you’re going to learn how to write 10000 as 10K, 1500 as 1.5k,  1500000 as 1.5m and so on. This can be useful in games where there isn’t much space to write the exact numbers, and this may look much cooler. So, let’s get going!

Test Project

For demonstrating this, I have created a test project where the numbers I type will appear with notations. It has just one object and a small room.

Create Event:

number = 0;
number_prefix = 0;
number_suffix = "";
number_output = "";

Here I’m just initializing the variables that I’m going to use. Two numeric variables and two strings. number will read the numbers I type, from the keyboard_string variable (the variable which stores whatever I type). number_prefix will store the prefix of the notated number (for example, 2, 1.5, 100 and so on) and number_suffix will store the notation (K or M or B). Then number_output will combine those who and show us the final number.

Draw Event:

draw_text(5, 5, keyboard_string);

draw_text(5, 37, number_output);

The first line will show me what I’ve typed and the second will show me the notated number.

The Main Part

Getting to the main part – this is where our number notations will be calculated.

Step Event:

number = real(keyboard_string);
number_prefix = number;
number_suffix = "";

Here I’m converting keyboard_string (what I type) into a real number using real() and then assigning the value to number. In your case you can just use your variable which you want to be notated (like number = score or number = money).

Then I’m assigning the number value to number_prefix, in case the number doesn’t turn out large enough for a notation, it must have some value to show. Same goes for number_suffix, which can stay empty if there’s no notation to be done.

Step Event: (Add the code)

if (number > 999 && number < 1000000){
 number_prefix = number/1000;
 number_suffix = "K";
}

This is pretty simple. If the number is greater than 999 (which means it is a thousand or more) and if it is lower than 1,000,000 (which means smaller than a million), it will set number_prefix to number divided by 1000 and set the number suffix to “K” – denoting a thousand.

Step Event: (Add the code)

if (number > 999999 && number < 1000000000){
 number_prefix = number/1000000;
 number_suffix = "M";
}

Similarly, here it checks if it is in the range of a million or more and less than a billion, and then sets the prefix and suffix accordingly.

Step Event: (Add the code)

if (number > 999999999){
 number_prefix = number/1000000000;
 number_suffix = "B";
}

Same goes for a billion, this one just doesn’t have a limit. Any number a billion or beyond will be notated as B.

Step Event: (Add the code)

number_output = string(number_prefix) + number_suffix;

Here I’m converting number_prefix, which is a numeric variable, to a string using string(), adding number_suffix to it, and assigning the result to number_output.

Execute!

gif1.gif

Works so well!

This gives us a number rounded off to two decimal digits. If you want to change this or have more control over the output (i.e., the number of digits/decimals), you can use string_format() instead of string() while converting number_prefix in the last line. Read about the function here.

Conclusion

It just became easier for you to show large values. It looks cool, too! wink.png

Download this test project

Need more help, or want to ask something? Use the comment section below or mail me at gurpreetsingh793@gmail.com. If it helped you out, why not share it with others and help them out too? 😉

Join your fellow GameMakers on GameMaker’s Guild (Facebook) (It has 6,758 6.75K members!) and on the GameMaker Community (YoYo Games forum).

Thanks for being here, and have a great day!

 

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s