PDA

Click to See Complete Forum and Search --> : 1st 2nd 3rd etc...


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?

MrPolite
Jul 7th, 2005, 08:18 PM
nice job, but I'd avoid conditional if statements. They're just more confusing and at times slower. It's better to have the code a bit longer but more clear in my opinion

wossname
Jul 12th, 2005, 05:38 AM
I thought conditional if's were only slower in VB6 (and therefore VB.Net), aren't they implemented differently in C# / C++?

?:

wossname
Jul 12th, 2005, 05:39 AM
It's better to have the code a bit longer but more clear in my opinion

I agree, but i was just trying to push the code as far as it would go :D

MrPolite
Jul 12th, 2005, 03:19 PM
I thought conditional if's were only slower in VB6 (and therefore VB.Net), aren't they implemented differently in C# / C++?

?:
hmm actually you're making me confused now:D
someone needs to look this up hehe, maybe you're right

jmcilhinney
Jul 29th, 2005, 01:58 AM
The C# conditional operator is not a function like IIf so is quite efficient in execution. I would say that, for the sake of clarity, it should only be used with relatively short statements or else written on two lines if possible, e.g.double z = x == 0 ? 0
: y / x;

StrangerInBeijing
Aug 3rd, 2005, 10:11 PM
only slower in VB6 (and therefore VB.Net)
?:

The IL created by the CLR for if statements in vb.net & c# are the same...remember i checked it a while ago.

jmcilhinney
Aug 3rd, 2005, 10:22 PM
The IL created by the CLR for if statements in vb.net & c# are the same...remember i checked it a while ago.I don't think he means straight If statements, but rather ?: vs IIf. One is an operator and one is a function so there will be a significant difference in IL.