Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
i need to display only last two digit of the value ab12345678, what code will be used to get this out put. i just put if function to make it run, infact i want system read value of X and take it last two digit and display it.

C#
// struct.cs
using System;
struct vu
{
    private int id;
    public int X


      {
        get
        {
            return id;
        }
        set
        {
            if (id < 100)
            id = value;
        }
    }
    public void DisplayX()
    {
        Console.WriteLine("Last Two Digit Of the ID is: {0}", id);
    }
}

class Abc
{
    public static void Main()
    {
        vu ss = new vu ();
        ss.X = ab12345678;
        ss.DisplayX();
    }
}
Posted
Comments
F-ES Sitecore 27-May-15 11:01am    
Use a combination of id.ToString().Length and id.ToString().Substring, or do it using maths via the % operator.
Maciej Los 27-May-15 11:18am    
Do not repost!
http://www.codeproject.com/Questions/995565/How-do-I-get-last-digit-of-the-id?arn=10

Try this:

C#
public void DisplayX()
{
    //Console.WriteLine("Last Two Digit Of the ID is: {0}", id);
    Console.WriteLine("Last Two Digit Of the ID is: {0:D2}", id % 100);
}
 
Share this answer
 
v3
Comments
Andy Lanng 27-May-15 11:03am    
love it! 5*
Leo Chapiro 27-May-15 12:58pm    
Thx Andy :)
Sascha Lefèvre 27-May-15 11:07am    
It's clever - but not 100% correct because it will only show one digit for multiples of 100.
Andy Lanng 27-May-15 11:16am    
and now it's perfect :Þ
Sergey Alexandrovich Kryukov 27-May-15 12:19pm    
"Perfect" would look like a kind of exaggeration. The solution is rather hard-coded...
Please see Solution 2, also not perfect... :-)
—SA
This is the improved solution, instead of the one which was auto-removed with your next question, due to some abuse reports:

First of all, the notion of decimal or hexadecimal digits is inapplicable to numeric objects, which are always "binary". Therefore, you can represent the numeric value to string and modify it. This is not the best solution in terms of performance, but it is most practical and easy to maintain. It can be something like
C#
static class Something {

    internal static string LastDigitsOfHexNumber(int value, byte numberOfDigits) {
        string result = string.Format(hexFormat, value);
        return result.Remove(0, maxNumberOfDigitsInHexInt - numberOfDigits);
    }

    const int maxNumberOfDigitsInHexInt = sizeof(int) * 2;
    static readonly string hexFormat =
       string.Format("{{0:X{0}}}", maxNumberOfDigitsInHexInt);

}
Note the constant and static readonly members. This is the way to avoid hard-coding of immediate constants 8 and "{0:X8}" and, at the same time, avoiding recalculation of these values on each call. Such techniques look more sophisticated, but they greatly improve maintenance.

Now, I would advise you to avoid both re-posting and self-answering, to avoid abuse reports. Your removed "answer" wasn't correct and wasn't even worse. More essentially, people generally are not really interested to see how you failed to solve some trivial problem and later solved it. It only adds garbage to the site.

Thank you for your understanding.

—SA
 
Share this answer
 
v2
Comments
Andy Lanng 27-May-15 12:25pm    
It's good. Very good. But _duDE_s solution is very pretty :D
5ed
Sergey Alexandrovich Kryukov 27-May-15 12:28pm    
Thank you very much, Andy.
As to that solution... to me, hard-coded elements are never pretty enough. :-)
—SA
Leo Chapiro 27-May-15 12:59pm    
The words of wisdom :) +5
Sergey Alexandrovich Kryukov 27-May-15 14:26pm    
Thank you very much.
—SA
Abhipal Singh 27-May-15 13:21pm    
5ed. Good solution!

Can we have like button on comments in Code Project :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900