Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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 an int.

int start = Convert.ToInt32(curr.ToString().Substring(0, 1)); 

While this does the job, it feels like there is probably a good, simple, math-based solution to such a problem. String manipulation feels clunky.

Edit: irrespective of speed differences, mystring[0] instead of Substring() is still just string manipulation

like image 557
Dinah Avatar asked Mar 31 '09 14:03

Dinah


People also ask

How do I find the first digit of an integer?

To finding first digit of a number is little expensive than last digit. To find first digit of a number we divide the given number by 10 until number is greater than 10. At the end we are left with the first digit.

How do I print the first digit of a number?

printf("First digit of the entered number is: %d \n", firstDigit); printf("Last digit of the entered number is: %d", lastDigit); // Displaying output printf("First digit of the entered number is: %d \n", firstDigit); printf("Last digit of the entered number is: %d", lastDigit);

Where is the first digit in a number?

The first digit 1 is in the Hundreds place and has a value of One hundred. The second digit 1 is in the Tens place and has a value of Ten. The digit 2 is in the Ones place and has a value of Two.


1 Answers

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

like image 147
John Rasch Avatar answered Oct 13 '22 04:10

John Rasch