Results 1 to 8 of 8

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

Threaded View

  1. #1

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

Posting Permissions

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



Click Here to Expand Forum to Full Width