1st 2nd 3rd etc...-VBForums
Results 1 to 8 of 8

Thread: 1st 2nd 3rd etc...

  1. #1

    Thread Starter
    Noodly Appendage wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,669

    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.

  2. #2
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    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!!

  3. #3

    Thread Starter
    Noodly Appendage wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,669

    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.

  4. #4

    Thread Starter
    Noodly Appendage wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,669

    Re: 1st 2nd 3rd etc...

    Quote 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

  5. #5
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: 1st 2nd 3rd etc...

    Quote 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!!

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    87,257

    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;

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: 1st 2nd 3rd etc...

    Quote 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.
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    87,257

    Re: 1st 2nd 3rd etc...

    Quote 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.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.