Introduction:
Ordinal numbers are crucial for expressing positions, rankings, or sequences in a more human-friendly manner. In this blog, we'll explore ordinal number conversion using the C# programming language.
Method 1: Using Conditional Statements
The first approach involves utilizing conditional statements to determine the appropriate ordinal suffix for a given number. This method is straightforward and involves analyzing the last digit of the number to apply the suitable suffix, such as "st," "nd," "rd," or "th." Below is a C# program that demonstrates this method:
using System;
class Program
{
static string ConvertToOrdinal(int number)
{
if (number >= 11 && number <= 13)
{
return number + "th";
}
switch (number % 10)
{
case 1: return number + "st";
case 2: return number + "nd";
case 3: return number + "rd";
default: return number + "th";
}
}
static void Main()
{
int num = 42;
string ordinal = ConvertToOrdinal(num);
Console.WriteLine($"The ordinal form of {num} is {ordinal}");
}
}
Output:
The ordinal form of 42 is 42nd
Method 2: Using Ternary Operator
The second method involves leveraging the ternary operator to create a concise and elegant solution. This operator allows us to make quick decisions based on conditions, making it suitable for ordinal number conversion. Here's a C# code snippet that demonstrates this approach:
using System;
class Program
{
static string ConvertToOrdinal(int number)
{
string suffix = (number >= 11 && number <= 13) ? "th"
: (number % 10 == 1) ? "st"
: (number % 10 == 2) ? "nd"
: (number % 10 == 3) ? "rd"
: "th";
return number + suffix;
}
static void Main()
{
int num = 73;
string ordinal = ConvertToOrdinal(num);
Console.WriteLine($"The ordinal form of {num} is {ordinal}");
}
}
Output:
The ordinal form of 73 is 73rd
Method 3: Using a Dictionary
The third method involves utilizing a dictionary to store the ordinal suffixes and their corresponding conditions. This approach strikes a balance between readability and flexibility. Here's a C# code snippet showcasing this technique:
using System;
using System.Collections.Generic;
class Program
{
static string ConvertToOrdinal(int number)
{
Dictionary<int, string> suffixes = new Dictionary<int, string>
{
{ 11, "th" }, { 12, "th" }, { 13, "th" },
{ 1, "st" }, { 2, "nd" }, { 3, "rd" },
};
int lastDigits = number % 100;
int lastDigit = number % 10;
return suffixes.ContainsKey(lastDigits) ? number + suffixes[lastDigits] : number + suffixes[lastDigit];
}
static void Main()
{
int num = 105;
string ordinal = ConvertToOrdinal(num);
Console.WriteLine($"The ordinal form of {num} is {ordinal}");
}
}
Output:
The ordinal form of 105 is 105th
Conclusion:
In this blog, we explored different methods for converting numbers to their ordinal forms in C#. We started with conditional statements, which provide a straightforward approach. Then, we explored the ternary operator for a more concise solution. Lastly, we demonstrated the use of a dictionary for a balanced blend of readability and flexibility.
Comments (0)