Convert Number To Ordinal - CSharp System

CSharp examples for System:DateTime Convert

Description

Convert Number To Ordinal

Demo Code


using System;/* w w  w  .  j  a  v  a  2 s. c o  m*/

public class Main{
        public static string ConvertToOrdinal(int number)
        {
            var ones = number % 10;
            var tens = Math.Floor(number / 10f) % 10;
            if (tens == 1)
                return number + "th";

            switch (ones)
            {
                case 1: return number + "st";
                case 2: return number + "nd";
                case 3: return number + "rd";
                default: return number + "th";
            }
        }
}

Related Tutorials