namespace MyApp.Extensions
{
using System;
using System.Globalization;
using System.Net;
public static class NumberExtensions
{
/// Appends the numeric ordinal.
/// The number.
/// A string.
public static string AppendNumericOrdinal(int num)
{
switch (num % 100)
{
case 11:
case 12:
case 13:
return num.ToString(CultureInfo.InvariantCulture) + "th";
}
switch (num % 10)
{
case 1:
return num.ToString(CultureInfo.InvariantCulture) + "st";
case 2:
return num.ToString(CultureInfo.InvariantCulture) + "nd";
case 3:
return num.ToString(CultureInfo.InvariantCulture) + "rd";
default:
return num.ToString(CultureInfo.InvariantCulture) + "th";
}
}
/// An extension method that converts an unsigned integer to an IP address.
/// The address to act on.
/// address as a string.
public static string UIntToIPAddress(this uint address)
{
return new IPAddress(BitConverter.GetBytes(address)).ToString();
}
/// Determines whether the specified number is prime.
/// The number.
/// true if prime, false if not.
public static bool IsPrime(this int number)
{
if ((number % 2) == 0)
{
return number == 2;
}
var sqrt = (int)Math.Sqrt(number);
for (var t = 3; t <= sqrt; t = t + 2)
{
if (number % t == 0) return false;
}
return number != 1;
}
}
}