wossname
Jul 4th, 2005, 06:10 AM
I have ported conipto's function (http://www.vbforums.com/showthread.php?p=2071447) 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
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);
}
Can this be done in less code?
Its just a function that adds a suffix to a number making it look like "1st", or "657th". Negative numbers also supported
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);
}
Can this be done in less code?