How can you get the first digit in an int (C#)?

后端 未结 25 2704
遇见更好的自我
遇见更好的自我 2020-12-02 05:43

In C#, what\'s the best way to get the 1st digit in an int? The method I came up with is to turn the int into a string, find the 1st char of the string, then turn it back to

相关标签:
25条回答
  • 2020-12-02 06:01

    This is what I usually do ,please refer my function below :

    This function can extract first number occurance from any string you can modify and use this function according to your usage

       public static int GetFirstNumber(this string strInsput)
        {
            int number = 0;
            string strNumber = "";
            bool bIsContNo = true;
            bool bNoOccued = false;
    
            try
            {
                var arry = strInsput.ToCharArray(0, strInsput.Length - 1);
    
                foreach (char item in arry)
                {
                    if (char.IsNumber(item))
                    {
                        strNumber = strNumber + item.ToString();
    
                        bIsContNo = true;
    
                        bNoOccued = true;
                    }
                    else
                    {
                        bIsContNo = false;
                    }
    
                    if (bNoOccued && !bIsContNo)
                    {
                        break;
                    }
    
    
                }
    
                number = Convert.ToInt32(strNumber);
    
            }
            catch (Exception ex)
            {
    
                return 0;
            }
    
            return number;
    
        }
    
    0 讨论(0)
  • 2020-12-02 06:03
    int i = 4567789;
    int digit1 = int.Parse(i.ToString()[0].ToString());
    
    0 讨论(0)
  • 2020-12-02 06:04
    int myNumber = 8383;
    char firstDigit = myNumber.ToString()[0];
    // char = '8'
    
    0 讨论(0)
  • 2020-12-02 06:05

    Very simple (and probably quite fast because it only involves comparisons and one division):

    if(i<10)
       firstdigit = i;
    else if (i<100)
       firstdigit = i/10;
    else if (i<1000)
       firstdigit = i/100;
    else if (i<10000)
       firstdigit = i/1000;
    else if (i<100000)
       firstdigit = i/10000;
    else (etc... all the way up to 1000000000)
    
    0 讨论(0)
  • Had the same idea as Lennaert

    int start = number == 0 ? 0 : number / (int) Math.Pow(10,Math.Floor(Math.Log10(Math.Abs(number))));
    

    This also works with negative numbers.

    0 讨论(0)
  • 2020-12-02 06:08

    Benchmarks

    Firstly, you must decide on what you mean by "best" solution, of course that takes into account the efficiency of the algorithm, its readability/maintainability, and the likelihood of bugs creeping up in the future. Careful unit tests can generally avoid those problems, however.

    I ran each of these examples 10 million times, and the results value is the number of ElapsedTicks that have passed.

    Without further ado, from slowest to quickest, the algorithms are:

    Converting to a string, take first character

    int firstDigit = (int)(Value.ToString()[0]) - 48;
    

    Results:

    12,552,893 ticks
    

    Using a logarithm

    int firstDigit = (int)(Value / Math.Pow(10, (int)Math.Floor(Math.Log10(Value))));
    

    Results:

    9,165,089 ticks
    

    Looping

    while (number >= 10)
        number /= 10;
    

    Results:

    6,001,570 ticks
    

    Conditionals

    int firstdigit;
    if (Value < 10)
         firstdigit = Value;
    else if (Value < 100)
         firstdigit = Value / 10;
    else if (Value < 1000)
         firstdigit = Value / 100;
    else if (Value < 10000)
         firstdigit = Value / 1000;
    else if (Value < 100000)
         firstdigit = Value / 10000;
    else if (Value < 1000000)
         firstdigit = Value / 100000;
    else if (Value < 10000000)
         firstdigit = Value / 1000000;
    else if (Value < 100000000)
         firstdigit = Value / 10000000;
    else if (Value < 1000000000)
         firstdigit = Value / 100000000;
    else
         firstdigit = Value / 1000000000;
    

    Results:

    1,421,659 ticks
    

    Unrolled & optimized loop

    if (i >= 100000000) i /= 100000000;
    if (i >= 10000) i /= 10000;
    if (i >= 100) i /= 100;
    if (i >= 10) i /= 10;
    

    Results:

    1,399,788 ticks
    

    Note:

    each test calls Random.Next() to get the next int

    0 讨论(0)
提交回复
热议问题