Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

For adding commas, if you're using javascript, you can simply use:

number.toLocaleString()

and it will return the number formatted for whatever the user's language prefers, eg. comma separation for US locale, period separation for many European locales, etc. You can pass a string to the function to specify the locale if you want to be opinionated, such as 'en-US' for US locales.

If you want to do scientific notation you could do:

number.toExponential()

If you want to use your own abbreviations, you could do something like:

const num = 1234567890;
const commaNum = number.toLocaleString('en-US'); // Returns 1,234,567,890 regardless of user's locale
const splitNum = commaNum.split(','); // Splits the string into an array of strings using the commas as the split points
const displayNum = splitNum[0]; // Set the display num to the most significant digits; 1 in this case
if (splitNum.length === 1) {
    return displayNum // No commas means no abbreviation necessary
}
else
if (splitNum.length === 2) {
    return displayNum + "K"; // 1 comma means we're dealling with thousands
}
else
if (splitNum.length === 3) {
    return displayNum + "M"; // 2 commas means we're dealling with millions
}
else
if (splitNum.length === 4) {
    return displayNum + "B"; // 3 commas means we're dealling with billions; which we are in this specific case
}
else
if (splitNum.length === 5) {
    // Continue the pattern until numbers get so large that scientific notation is the only thing that's reasonable any more...
}

The above code will only return 1-3 whole number digits followed by an abbreviation letter.

(+1)

I'm using Gamemaker which does GML, and am not super good with using strings yet, but am doing my best to learn and improve, so will hpoefully be able to put this to use soon. (Since I'm sure it wouldn't be too hard to find an equivilent function for GML)