I have ported conipto's function to C# (mainly for my own practice really) with a few modifications.
Its just a function that adds a suffix to a number making it look like "1st", or "657th". Negative numbers also supported
Can this be done in less code?Code:private 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); }


) with a few modifications.
Reply With Quote

