How do you do to get only 6 from 2006

I need the last number of the year to use it to for something else. How do I do it

Answer this question

How do you do to get only 6 from 2006

  • mgalliers

    Whats wrong with:

    int lastDigit = 2006 % 100;


  • chandra shekhar

    If you think 1 digit is not enough, you can always get more digits by changing the 10 in the above fragment into 100 (2 digits; 00-99) or 1000 (3 digits).


  • levogiro

     ThE_lOtUs wrote:

    For all millenium (if it's a number)

    int subYear = year - ((int)(year/10)*10)

    I'm not a mind reader, but I think YoK was essentially just trying to get the sub year of the current millenium.

    Your code only works for years 1-9 in any given millnenium.

    There probably is a way to do this for any millenium, but I'm too frazzled to think about it .



  • Michael Koster

    For all millenium (if it's a number)

    int subYear = year - ((int)(year/10)*10)



  • codelinezero

    you can try something like this

    string sNumber = "";

    int iNumber = 0;

    DateTime dt = DateTime.Now;

    sNumber = Convert.ToString(dt.year);

    iNumber = Convert.ToInt32(sNumber.Substring(3,1));

    Hope this helps.

     

     


  • above8848

    If he really does want the "last number of the year" (I assume that means "last digit of the year"), it would have to be:

    int lastDigit = year % 10;

    But if the year is in string form, it would have to be:

    string lastDigit = year.Substring(year.Length-1, 1);

    Or if you KNOW it's a 4-character year:

    string lastDigit = year.Substring(3, 1);


  • GaryKnowles

    If the year is a number, you can achieve this by just subtracting 2000. This approach will work for all years in this millenium at least .

    If it's a string, you could do the following:

    int subYear = int.Parse(year) - 2000;



  • How do you do to get only 6 from 2006