如何仅显示最后两位数字 [英] How do I display last two digit only

查看:117
本文介绍了如何仅显示最后两位数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需要显示值ab12345678的最后两位数字,将使用什么代码来获得此输出。我只是把if函数让它运行,实际上我想要系统读取X值并取两个数字并显示它。



  //   struct.cs  
使用系统;
struct vu
{
private int id;
public int X


{
get
{
return id;
}
set
{
if (id < 100
id = value < /跨度>;
}
}
public void DisplayX()
{
Console.WriteLine( ID的最后两位是:{0},id);
}
}

class abc
{
public static void Main()
{
vu ss = new vu();
ss.X = ab12345678;
ss.DisplayX();
}
}

解决方案

试试这个:



  public   void  DisplayX()
{
// Console.WriteLine(ID的最后两位数为:{0},id);
Console.WriteLine( 最后两位数的ID是:{0:D2} id% 10 0 );
}


这是改进的解决方案,而不是由于某些问题而自动删除的解决方案滥用报告:



首先,十进制或十六进制数字的概念不适用于数字对象,它们总是二进制。因此,您可以将数值表示为字符串并对其进行修改。这在性能方面不是最佳解决方案,但它最实用且易于维护。它可以是类似

  static   class 某事{

内部 静态 string LastDigitsOfHexNumber( int value byte numberOfDigits){
string result = string .Format(hexFormat,);
return result.Remove( 0 ,maxNumberOfDigitsInHexInt - numberOfDigits);
}

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

}

注意常量和静态只读成员。这是避免对立即常量 8和{0:X8}进行硬编码的方法,同时避免在每次调用时重新计算这些值。这些技术看起来更复杂,但它们大大改善了维护。



现在,我建议你避免重新发布和自我回答,以避免滥用报告。你删除的答案不正确,甚至更糟。更重要的是,人们通常并不真正有兴趣看到你如何解决一些微不足道的问题并在以后解决它。它只会给网站增加垃圾。



感谢您的理解。



- SA

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.

// 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();
    }
}

解决方案

Try this:

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);
}


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

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


这篇关于如何仅显示最后两位数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆