Liberty Unleashed

Scripting => Script Snippets => Topic started by: Windlord on April 25, 2011, 03:37:00 am

Title: Add "st", "nd", "rd", "th"
Post by: Windlord on April 25, 2011, 03:37:00 am
Code: (squirrel) [Select]
// This function returns 1st/2nd/3rd/4th from an integer
function GetNth ( num )
{
local lastdigs = num % 100;
switch ( lastdigs )
{
case 11: return num +"th"; // Catch out 11, 111
case 12: return num +"th"; // Catch out 12, 112
case 13: return num +"th"; // Catch out 13, 113
default: // Everything else should be normal...
lastdigs = num % 10; // Get last digit
switch ( lastdigs )
{
case 1: return num +"st";
case 2: return num +"nd";
case 3: return num +"rd";
default: return num +"th";
}
}
}
Title: Re: Add "st", "nd", "rd", "th"
Post by: Windlord on May 05, 2011, 05:00:03 pm
Thanks to Mike for pointing it out:

Now GetNth returns 111th, 11th, 112th, 12th, 113th, and 13th
instead of 111st, 11st, 112nd, 12nd, 113rd, 13rd.