• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Adding Ordinal Indicators to numbers (1st, 2nd, 3rd, 4th etc)

MrDave

Member
GM Version: ALL
Target Platform: ALL
Download: N/A
Links: http://www.davetech.co.uk/gamemakerordinalindicator

Ordinal Indicators are the letters in 1st, 2nd, 3rd, 4th etc.

I’ve written a quick script for people wanting to add the correct letters to the end of an integer.

This isn’t going to be helpful for most people, but it can be tricky for people that haven’t done it before.

Code:
/// ordinalindicator(int) returns the int as a str with st, nd, rd or th at the end

if( argument0 <= 0 ) return argument0; //technically ordinals don't exist for <= 0

if ((argument0 mod 100) >= 11 and (argument0 mod 100) <= 13) {
    return string(argument0) + "th";
}

switch(argument0 mod 10)
{
    case 1:
        return string(argument0) + "st";
    case 2:
        return string(argument0) + "nd";
    case 3:
        return string(argument0) + "rd";
    default:
        return string(argument0) + "th";
}
How to use it:
Code:
ordinalindicator(1);    // outputs: "1st"
ordinalindicator(2);    // outputs: "2nd"
ordinalindicator(3);    // outputs: "3rd"
 
S

Snayff

Guest
That's nice to know buddy, thank you. Not sure how often it would come up but when it does it would be super useful!
 

gnysek

Member
Code:
/// @desc ordinalindicator(int) returns the int as a str with st, nd, rd or th at the end
/// @param integer
var s = ["st" ,"nd", "rd"], v = ((argument0 + 90) % 100-10) % 10;
return string(argument0) + ( (v and v <= 3) ? s[v-1] : "th");
:)
 
Top