Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Ordinal Function - .NET Framework

RSS
Modified on Fri, Aug 26, 2011, 3:17 PM by Administrator Categorized as ┬ĚNet Framework
See also: Ordinal Function - SQL Server

C#

public static string ToOrdinal(this int input)
{
    var mod100 = input % 100;
    var mod10 = input % 10;
    var result = input.ToString();

    if (mod10 == 1 & mod100 != 11)
        result += "st";
    else if (mod10 == 2 & mod100 != 12)
        result += "nd";
    else if (mod10 == 3 & mod100 != 13)
        result += "rd";
    else
        result += "th";

    return result;
}

Visual Basic

<Extension()>
Function GetOrdinal(n As Integer) As String

    Dim mod100 As Integer = n Mod 100
    Dim mod10 As Integer = n Mod 10
    Dim result As String = n.ToString()

    If mod10 = 1 And mod100 <> 11 Then
        result += "st"
    ElseIf mod10 = 2 And mod100 <> 12 Then
        result += "nd"
    ElseIf mod10 = 3 And mod100 <> 13 Then
        result += "rd"
    Else
        result += "th"
    End If

    Return result

End Function

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2018, Patrick Jasinski.