-
Jul 4th, 2005, 05:10 AM
#1
1st 2nd 3rd etc...
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
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);
}
Can this be done in less code?
Last edited by wossname; Jul 4th, 2005 at 04:38 PM.
I don't live here any more.
-
Jul 7th, 2005, 07:18 PM
#2
Re: 1st 2nd 3rd etc...
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
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Jul 12th, 2005, 04:38 AM
#3
Re: 1st 2nd 3rd etc...
I thought conditional if's were only slower in VB6 (and therefore VB.Net), aren't they implemented differently in C# / C++?
?:
I don't live here any more.
-
Jul 12th, 2005, 04:39 AM
#4
Re: 1st 2nd 3rd etc...
Originally Posted by MrPolite
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
-
Jul 12th, 2005, 02:19 PM
#5
Re: 1st 2nd 3rd etc...
Originally Posted by wossname
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
someone needs to look this up hehe, maybe you're right
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Jul 29th, 2005, 12:58 AM
#6
Re: 1st 2nd 3rd etc...
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.
Code:
double z = x == 0 ? 0
: y / x;
-
Aug 3rd, 2005, 09:11 PM
#7
Frenzied Member
Re: 1st 2nd 3rd etc...
Originally Posted by wossname
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.
-
Aug 3rd, 2005, 09:22 PM
#8
Re: 1st 2nd 3rd etc...
Originally Posted by StrangerInBeijing
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|