Add Ordinal Suffix
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public static class DataConversion
{
public static String AddOrdinalSuffix(int num)
{
//can handle negative numbers (-1st, -12th, -21st)
int last2Digits = Math.Abs(num % 100);
int lastDigit = last2Digits % 10;
//the only nonconforming set is numbers ending in <...eleventh, ...twelfth, ...thirteenth>
return num.ToString() + "thstndrd".Substring((last2Digits > 10 && last2Digits < 14) || lastDigit > 3 ? 0 : lastDigit * 2, 2);
}
}
Related examples in the same category