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

<pre><code>   1 to 1st
   2 to 2nd
   3 to 3rd
   4 to 4th
  11 to 11th
 101 to 101st
 111 to 111th
</code></pre>

<p><a href="http://www.dotnet-friends.com/fastcode/csharp/fastcodeincsc3bd4149-03d0-40fe-90fd-63bcee77b43e.aspx" rel="noreferrer">This link</a> 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.</p>

<p><strong>Solution</strong></p>

<p>Scott Hanselman's answer is the accepted one because it answers the question directly.</p>

<p>For a solution however, see <a href="https://stackoverflow.com/questions/69262/is-there-an-easy-way-in-net-to-get-st-nd-rd-and-th-endings-for-numbers#69284">this great answer</a>.</p>

[{'answer_id': 69279, 'author': 'Scott Hanselman', 'author_id': 6380, 'author_profile': 'https://Stackoverflow.com/users/6380', 'pm_score': 7, 'selected': True, 'text': '<p>No, there is no inbuilt capability in the .NET Base Class Library.</p>\n'}, {'answer_id': 69284, 'author': 'nickf', 'author_id': 9021, 'author_profile': 'https://Stackoverflow.com/users/9021', 'pm_score': 6, 'selected': False, 'text': '<p>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.</p>\n\n<pre><code>function ordinal($num) {\n    $ones = $num % 10;\n    $tens = floor($num / 10) % 10;\n    if ($tens == 1) {\n        $suff = "th";\n    } else {\n        switch ($ones) {\n            case 1 : $suff = "st"; break;\n            case 2 : $suff = "nd"; break;\n            case 3 : $suff = "rd"; break;\n            default : $suff = "th";\n        }\n    }\n    return $num . $suff;\n}\n</code></pre>\n'}, {'answer_id': 69287, 'author': 'mjallday', 'author_id': 6084, 'author_profile': 'https://Stackoverflow.com/users/6084', 'pm_score': 4, 'selected': False, 'text': '<p>This has already been covered but I\'m unsure how to link to it. Here is the code snippit:</p>\n\n<pre><code>    public static string Ordinal(this int number)\n    {\n        var ones = number % 10;\n        var tens = Math.Floor (number / 10f) % 10;\n        if (tens == 1)\n        {\n            return number + "th";\n        }\n\n        switch (ones)\n        {\n            case 1: return number + "st";\n            case 2: return number + "nd";\n            case 3: return number + "rd";\n            default: return number + "th";\n        }\n    }\n</code></pre>\n\n<p>FYI: This is as an extension method. If your .NET version is less than 3.5 just remove the this keyword</p>\n\n<p>[EDIT]: Thanks for pointing that it was incorrect, that\'s what you get for copy / pasting code :)</p>\n'}, {'answer_id': 69293, 'author': 'Hugh Buchanan', 'author_id': 9307, 'author_profile': 'https://Stackoverflow.com/users/9307', 'pm_score': -1, 'selected': False, 'text': "<p>I think the ordinal suffix is hard to get... you basically have to write a function that uses a switch to test the numbers and add the suffix.</p>\n\n<p>There's no reason for a language to provide this internally, especially when it's locale specific.</p>\n\n<p>You can do a bit better than that link when it comes to the amount of code to write, but you have to code a function for this...</p>\n"}, {'answer_id': 69328, 'author': 'Scott Dorman', 'author_id': 1559, 'author_profile': 'https://Stackoverflow.com/users/1559', 'pm_score': 6, 'selected': False, 'text': '<p>@nickf: Here is the PHP function in C#:</p>\n\n<pre><code>public static string Ordinal(int number)\n{\n    string suffix = String.Empty;\n\n    int ones = number % 10;\n    int tens = (int)Math.Floor(number / 10M) % 10;\n\n    if (tens == 1)\n    {\n        suffix = "th";\n    }\n    else\n    {\n        switch (ones)\n        {\n            case 1:\n                suffix = "st";\n                break;\n\n            case 2:\n                suffix = "nd";\n                break;\n\n            case 3:\n                suffix = "rd";\n                break;\n\n            default:\n                suffix = "th";\n                break;\n        }\n    }\n    return String.Format("{0}{1}", number, suffix);\n}\n</code></pre>\n'}, {'answer_id': 386318, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': -1, 'selected': False, 'text': '<pre><code>else if (choice==\'q\')\n{\n    qtr++;\n\n    switch (qtr)\n    {\n        case(2): strcpy(qtrs,"nd");break;\n        case(3):\n        {\n           strcpy(qtrs,"rd");\n           cout&lt;&lt;"End of First Half!!!";\n           cout&lt;&lt;" hteam "&lt;&lt;"["&lt;&lt;hteam&lt;&lt;"] "&lt;&lt;hs;\n           cout&lt;&lt;" vteam "&lt;&lt;" ["&lt;&lt;vteam;\n           cout&lt;&lt;"] ";\n           cout&lt;&lt;vs;dwn=1;yd=10;\n\n           if (beginp==\'H\') team=\'V\';\n           else             team=\'H\';\n           break;\n       }\n       case(4): strcpy(qtrs,"th");break;\n</code></pre>\n'}, {'answer_id': 611179, 'author': 'redcalx', 'author_id': 15703, 'author_profile': 'https://Stackoverflow.com/users/15703', 'pm_score': 3, 'selected': False, 'text': "<p>Here's a Microsoft SQL Server Function version:</p>\n\n<pre><code>CREATE FUNCTION [Internal].[GetNumberAsOrdinalString]\n(\n    @num int\n)\nRETURNS nvarchar(max)\nAS\nBEGIN\n\n    DECLARE @Suffix nvarchar(2);\n    DECLARE @Ones int;  \n    DECLARE @Tens int;\n\n    SET @Ones = @num % 10;\n    SET @Tens = FLOOR(@num / 10) % 10;\n\n    IF @Tens = 1\n    BEGIN\n        SET @Suffix = 'th';\n    END\n    ELSE\n    BEGIN\n\n    SET @Suffix = \n        CASE @Ones\n            WHEN 1 THEN 'st'\n            WHEN 2 THEN 'nd'\n            WHEN 3 THEN 'rd'\n            ELSE 'th'\n        END\n    END\n\n    RETURN CONVERT(nvarchar(max), @num) + @Suffix;\nEND\n</code></pre>\n"}, {'answer_id': 9290324, 'author': 'avenmore', 'author_id': 37660, 'author_profile': 'https://Stackoverflow.com/users/37660', 'pm_score': 2, 'selected': False, 'text': "<p>I know this isn't an answer to the OP's question, but because I found it useful to lift the SQL Server function from this thread, here is a Delphi (Pascal) equivalent:</p>\n\n<pre><code>function OrdinalNumberSuffix(const ANumber: integer): string;\nbegin\n  Result := IntToStr(ANumber);\n  if(((Abs(ANumber) div 10) mod 10) = 1) then // Tens = 1\n    Result := Result + 'th'\n  else\n    case(Abs(ANumber) mod 10) of\n      1: Result := Result + 'st';\n      2: Result := Result + 'nd';\n      3: Result := Result + 'rd';\n      else\n        Result := Result + 'th';\n    end;\nend;\n</code></pre>\n\n<p>Does ..., -1st, 0th make sense?</p>\n"}, {'answer_id': 19553611, 'author': 'Shahzad Qureshi', 'author_id': 2719563, 'author_profile': 'https://Stackoverflow.com/users/2719563', 'pm_score': 6, 'selected': False, 'text': '<p>Simple, clean, quick</p>\n\n<pre><code>    private static string GetOrdinalSuffix(int num)\n    {\n        string number = num.ToString();\n        if (number.EndsWith("11")) return "th";\n        if (number.EndsWith("12")) return "th";\n        if (number.EndsWith("13")) return "th";\n        if (number.EndsWith("1")) return "st";\n        if (number.EndsWith("2")) return "nd";\n        if (number.EndsWith("3")) return "rd";\n        return "th";\n    }\n</code></pre>\n\n<p>Or better yet, as an extension method</p>\n\n<pre><code>public static class IntegerExtensions\n{\n    public static string DisplayWithSuffix(this int num)\n    {\n        string number = num.ToString();\n        if (number.EndsWith("11")) return number + "th";\n        if (number.EndsWith("12")) return number + "th";\n        if (number.EndsWith("13")) return number + "th";\n        if (number.EndsWith("1")) return number + "st";\n        if (number.EndsWith("2")) return number + "nd";\n        if (number.EndsWith("3")) return number + "rd";\n        return number + "th";\n    }\n}\n</code></pre>\n\n<p>Now you can just call </p>\n\n<pre><code>int a = 1;\na.DisplayWithSuffix(); \n</code></pre>\n\n<p>or even as direct as </p>\n\n<pre><code>1.DisplayWithSuffix();\n</code></pre>\n'}, {'answer_id': 20550095, 'author': 'Frank Hoffman', 'author_id': 747173, 'author_profile': 'https://Stackoverflow.com/users/747173', 'pm_score': 1, 'selected': False, 'text': '<p>Another flavor:</p>\n\n<pre><code>/// &lt;summary&gt;\n/// Extension methods for numbers\n/// &lt;/summary&gt;\npublic static class NumericExtensions\n{\n    /// &lt;summary&gt;\n    /// Adds the ordinal indicator to an integer\n    /// &lt;/summary&gt;\n    /// &lt;param name="number"&gt;The number&lt;/param&gt;\n    /// &lt;returns&gt;The formatted number&lt;/returns&gt;\n    public static string ToOrdinalString(this int number)\n    {\n        // Numbers in the teens always end with "th"\n\n        if((number % 100 &gt; 10 &amp;&amp; number % 100 &lt; 20))\n            return number + "th";\n        else\n        {\n            // Check remainder\n\n            switch(number % 10)\n            {\n                case 1:\n                    return number + "st";\n\n                case 2:\n                    return number + "nd";\n\n                case 3:\n                    return number + "rd";\n\n                default:\n                    return number + "th";\n            }\n        }\n    }\n}\n</code></pre>\n'}, {'answer_id': 20610293, 'author': 'Faust', 'author_id': 613004, 'author_profile': 'https://Stackoverflow.com/users/613004', 'pm_score': 0, 'selected': False, 'text': '<pre><code>public static string OrdinalSuffix(int ordinal)\n{\n    //Because negatives won\'t work with modular division as expected:\n    var abs = Math.Abs(ordinal); \n\n    var lastdigit = abs % 10; \n\n    return \n        //Catch 60% of cases (to infinity) in the first conditional:\n        lastdigit &gt; 3 || lastdigit == 0 || (abs % 100) - lastdigit == 10 ? "th" \n            : lastdigit == 1 ? "st" \n            : lastdigit == 2 ? "nd" \n            : "rd";\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/69262', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/364/']