Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an easy way in .NET to get "st", "nd", "rd" and "th" endings for numbers? [duplicate]

I am wondering if there is a method or format string I'm missing in .NET to convert the following:

   1 to 1st    2 to 2nd    3 to 3rd    4 to 4th   11 to 11th  101 to 101st  111 to 111th 

This link has a bad example of the basic principle involved in writing your own function, but I am more curious if there is an inbuilt capacity I'm missing.

Solution

Scott Hanselman's answer is the accepted one because it answers the question directly.

For a solution however, see this great answer.

like image 679
Matt Mitchell Avatar asked Sep 16 '08 03:09

Matt Mitchell


People also ask

How do you use st nd th?

When writing ordinal numbers such as 1st, 2nd, 3rd, etc. you should use the last two letters on the word as it would be if you wrote out the whole word. Below are the ordinal numbers both written out and with digits for 1-20. As you can see, 1st, 2nd, and 3rd use -st, -nd, and -rd, but 4th-20th use -th.

What is an ordinal C#?

An ordinal number gives the position of something in a list: 0th, 1st, 2nd, 3rd, 4th, 5th,... The suffixes are derived from how ordinal numbers are written: first, second, third, fourth, fifth, ...


2 Answers

It's a function which is a lot simpler than you think. Though there might be a .NET function already in existence for this, the following function (written in PHP) does the job. It shouldn't be too hard to port it over.

function ordinal($num) {     $ones = $num % 10;     $tens = floor($num / 10) % 10;     if ($tens == 1) {         $suff = "th";     } else {         switch ($ones) {             case 1 : $suff = "st"; break;             case 2 : $suff = "nd"; break;             case 3 : $suff = "rd"; break;             default : $suff = "th";         }     }     return $num . $suff; } 
like image 144
nickf Avatar answered Sep 27 '22 16:09

nickf


Simple, clean, quick

    private static string GetOrdinalSuffix(int num)     {         string number = num.ToString();         if (number.EndsWith("11")) return "th";         if (number.EndsWith("12")) return "th";         if (number.EndsWith("13")) return "th";         if (number.EndsWith("1")) return "st";         if (number.EndsWith("2")) return "nd";         if (number.EndsWith("3")) return "rd";         return "th";     } 

Or better yet, as an extension method

public static class IntegerExtensions {     public static string DisplayWithSuffix(this int num)     {         string number = num.ToString();         if (number.EndsWith("11")) return number + "th";         if (number.EndsWith("12")) return number + "th";         if (number.EndsWith("13")) return number + "th";         if (number.EndsWith("1")) return number + "st";         if (number.EndsWith("2")) return number + "nd";         if (number.EndsWith("3")) return number + "rd";         return number + "th";     } } 

Now you can just call

int a = 1; a.DisplayWithSuffix();  

or even as direct as

1.DisplayWithSuffix(); 
like image 30
Shahzad Qureshi Avatar answered Sep 27 '22 15:09

Shahzad Qureshi