Search this blog

Wednesday, October 6, 2010

C#: Find Century from Year

Today one of friend asked me, how to find century from the year thru C# code. The following code snippet helps:


int year = 2010;
int cent = year / 100;
year %= 100;


if (year > 0)
cent = cent + 1;


Console.WriteLine("{0}C",cent);

Output:
21C

1 comment:

  1. result = year.ToString().Length == 1 || year.ToString().Length == 2 ? "1" : (Convert.ToInt32(year.ToString().Substring(0, (year.ToString().Length - 2))) + 1).ToString();

    ReplyDelete