(int)1.IntegerTrail(); //returns "1st"
(int)2.IntegerTrail(); //returns "2nd"
(int)12.IntegerTrail(); //returns "12th"
(int)111.IntegerTrail(); //returns "111th"
xxxxxxxxxx
public static string IntegerTrail(this int number)//custom function to add st,nd and rd
{
int numtend = number + 1000;//adding 1000 to number so remove function doesn't get a negative
string teens = numtend.ToString();
teens = teens.Remove(0, teens.Length - 2);
if (teens.IndexOf("1") == 0)
{//checking to see if there's a 1 in the ten's place
return number.ToString() + "th";
}
else
{
string numberstring = number.ToString();
int n = Int32.Parse(numberstring.Remove(0, numberstring.Length - 1));//removing all but last charater in string
switch (n)
{
case 1:
return number.ToString() + "st";
case 2:
return number.ToString() + "nd";
case 3:
return number.ToString() + "rd";
default:
return number.ToString() + "th";//break not needed because of return
}
}
}
swap out the integer for a double or float or generic type and it should still work. That said, I haven't tried it myself though
xxxxxxxxxx
using System.IO;
using System;
public class Program {
public static void Main() {
// long suffix
long val1 = 29345L;
Console.WriteLine(val1);
// decimal suffix
decimal val5 = 3245.5678M;
Console.WriteLine(val5);
// double suffix
double val2 = 297.325D;
Console.WriteLine(val2);
// float suffix
float val3 = 250.35F;
Console.WriteLine(val3);
// unsigned suffix
uint val4 = 3456U;
Console.WriteLine(val4);
}
}
//can work in upper or lower case, but stylistically uppercase
//literals are tradition and preferred.