Add th, st, nd, rd to number - CSharp System

CSharp examples for System:String Number

Description

Add th, st, nd, rd to number

Demo Code


using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*w w w  .ja  va 2 s. co  m*/

public class Main{
        public static string AddIth(this string s)
        {
            var check = s.Last().ToString();

            if (s.Length >= 2)
            {
                check = s.Substring(s.Length - 2);
            }

            switch (check)
            {
                case "11":
                case "12":
                case "13":
                    return s + "th";
            }

            switch (check.Last())
            {
                case '1':
                    return s + "st";
                case '2':
                    return s + "nd";
                case '3':
                    return s + "rd";
                default:
                    return s + "th";
            }
        }
}

Related Tutorials