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

Posted by Dinah on Stack Overflow See other posts from Stack Overflow or by Dinah
Published on 2009-03-31T14:45:05Z Indexed on 2010/04/24 17:13 UTC
Read the original article Hit count: 193

Filed under:

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

© Stack Overflow or respective owner

Related posts about c#