GML [solved] Adding commas into score?

hdarren

Member
Hi. I want to add commas into my score. So instead of saying "3000000" it says "3,000,000". Is there an easy way of doing this automatically? Thank you.
 
The easiest way is to convert the number to a string. Then apply custom formatting to the string to add in the commas.

Ex, 3000000 would be 7 characters. This is what the code would look like to convert your example.

formatted_score_string =
string_char_at(1) + "," +
string_copy(score_string, 2, 3) + "," +
string_copy(score_string, 5, 3);

Here is a link to the GameMaker documentation on string functions.
https://docs.yoyogames.com/source/dadiospice/002_reference/strings/index.html
 

Dmi7ry

Member
Code:
/// num_separator(value, separator, digits)
// num_separator(12345678, "_", 3); // Result: 12_345_678

var value = string(round(argument0));
var sep = argument1;
var digits = argument2 - 1;

var res = "";

var cnt = 0;
for (var i=string_length(value); i>0; i--)
{
    res = string_char_at(value, i) + res;
    if cnt++ == digits and i > 1
    {
        cnt = 0;
        res = sep + res;
    }
}

return res;
 
Top